//
//  SHOrdersListView.swift
//  ServiceHelper
//
//  Created by Xuedan on 15/12/12.
//  Copyright © 2015年 Stan Wu. All rights reserved.
//

import UIKit
import AVFoundation

protocol SHOrdersListDelegate{
    func allOrdesUpdated(order: NSDictionary?)
    func showSelectedTakeCarPerson()
    func hiddenTakeCarPerson()
    
    func thePreviousStepClick(number:NSNumber?)
    func theNectStepClick(number:NSNumber?)
    func hideOrderDetailView(number:NSNumber?)
}

class SHOrdersListView: UIView,UITableViewDelegate,UITableViewDataSource,UIPopoverControllerDelegate,AVAudioPlayerDelegate,UIAlertViewDelegate,UIActionSheetDelegate,SWImageViewDelegate,SDPhotoBrowserDelegate,AVCaptureMetadataOutputObjectsDelegate{
    
    
    var vDetail:UIView!
    var tvList:UITableView!
    var pop: UIPopoverController!
    var player: AVAudioPlayer?
    var btnPlay: UIButton!
    var btnSelected:UIButton!
    var btnCancel:UIButton!
    var btnChangeTime:UIButton!
    var btnMore:UIButton!
    var btnReset:UIButton!
    var refreshController: UIRefreshControl!
    var lblMoneyCountTitle :UILabel!
    var lblMoneyCount :UILabel!
    
    var successView:UILabel!
    
    
    var vFooterBG:UIView!
    
    var selectedDate: NSDate?
    
    var strNew :NSString!
    var strOrderId :NSString!
    var strOrderStatus :NSString!
    var dealer_is_update:NSString!
    var orderType :NSString!
    
    var strShowType :NSString!
    var lblOrderNumber :UILabel!
    var lblOrderStatus :UILabel!
    var lblNotificationTime :UILabel!
    var btnShowDetail:UIButton!
    
    var aryStatusDetail:NSArray?
    var cellRowThreeHeight: CGFloat = 0
    var indexNumber: NSNumber!
    
    var workorder : NSString!
    var carstatus : NSInteger!
    
    var nothingView : UIView!
    var cancelImageView : UIImageView!
    
    
    var number :NSNumber?{
        didSet{
            indexNumber = number
        }
        
    }
    var strType: NSString?{
        didSet{
            
            strShowType = strType
        }
    }
    var str: NSString?{
        didSet{
            strNew = str!
            switch strNew{
            case "全部订单":break
            case "等待确认":break
            default:
                break
                
            }
        }
    }
    
    var OrdersInfo: NSDictionary?{
        didSet{
            cancelImageView.hidden = true
            if OrdersInfo != nil{
                tvList.hidden = false
                vFooterBG.hidden = false
            }else{
                tvList.hidden = true
                vFooterBG.hidden = true
            }
                if self.OrdersInfo != nil{
                    strOrderStatus = self.OrdersInfo?.objectForKey("order_status") as? String!
                    orderType = self.OrdersInfo?.objectForKey("pick_type") as? String!
                    
                    aryStatusDetail = self.OrdersInfo?.objectForKey("status_detail") as? NSArray
                    let ary = OrdersInfo?.objectForKey("status_detail")
                    
                    if let strdealer_is_update = self.OrdersInfo?.objectForKey("dealer_is_update") as! String!{
                        dealer_is_update = strdealer_is_update
                    }else{
                        dealer_is_update = "0"
                    }
                    
                    if ary?.count > 0{
                         let idx = (ary?.count)!-1
                        lblOrderStatus?.text = ary?[(Int)(idx)].objectForKey("head") as? String!
                    }
                    
                    var getPrice :Float
                    if let get_price = OrdersInfo?.objectForKey("pay_info")?.objectForKey("get_price"){
                        getPrice = get_price.floatValue
                    }else{
                        getPrice = 0
                    }
                    
                    var sendprice :Float
                    if let send_price = OrdersInfo?.objectForKey("pay_info")?.objectForKey("send_price"){
                        sendprice = send_price.floatValue
                    }else{
                        sendprice = 0
                    }
                    
                    var discount :Float
                    if let discount_price = OrdersInfo?.objectForKey("pay_info")?.objectForKey("discount"){
                        discount = discount_price.floatValue
                    }else{
                        discount = 0
                    }
                    
                    var servicePrice :Float
                    if let price_price = OrdersInfo?.objectForKey("pay_info")?.objectForKey("price"){
                        servicePrice = price_price.floatValue
                    }else{
                        servicePrice = 0
                    }
                    
                    
                      var  payCount = (Float)(servicePrice + getPrice + sendprice - discount)
                    if strOrderStatus == "9" || strOrderStatus == "10" || strOrderStatus == "11" || strOrderStatus == "12" || strOrderStatus == "16" || strOrderStatus == "4" || strOrderStatus == "5"{
                        if payCount <= 0{
                            payCount = 0
                            lblMoneyCount.hidden = false
                        }else{
                            lblMoneyCount.hidden = false
                        }
                    }else{
                       lblMoneyCount.hidden = true
                    }
                    
                    print(payCount)
//                    btnVoice.setTitle(String(format: "%.0lf\"", player!.duration), forState: .Normal)
//                    lblMoneyCount.text = "￥\(payCount)"
                    lblMoneyCount.text = String(format: "￥%.2f元", payCount)
                    //                    if let price = self.OrdersInfo?.objectForKey("price")  {
//                        lblMoneyCount.text = "￥\(price)"
//                    }else{
//                         self.lblMoneyCount.text = nil
//                    }
                    
                    let orderStatus =  strOrderStatus.intValue
                    btnReset.userInteractionEnabled = true
                    cancelImageView.hidden = true
                    switch orderStatus{
                    case 1:
                        btnCancel.userInteractionEnabled = true
                        btnCancel.alpha = 1
                        btnCancel.hidden = false
                        btnChangeTime.hidden = false
                        if dealer_is_update == "1"{
                            btnChangeTime.userInteractionEnabled = true
                            btnChangeTime.alpha = 1
                        }else{
                            btnChangeTime.userInteractionEnabled = false
                            btnChangeTime.hidden = true
                            btnChangeTime.alpha = 0.4
                        }
                        
                        if let pickType = OrdersInfo?.objectForKey("pick_type")?.intValue{
                            if 1==pickType{
                                btnReset.setTitle("车已到店", forState: .Normal)
                            }else{
                                btnReset.setTitle("确认订单", forState: .Normal)
                            }
                        }else{
                            btnReset.setTitle("车已到店", forState: .Normal)
                        }
                        
                        btnReset.userInteractionEnabled = true
                        btnReset.hidden = false
                        vFooterBG.hidden = false
                        btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                    case 2:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        btnReset.setTitle("变更订单", forState: .Normal)
                        btnReset.hidden = true
                        vFooterBG.hidden = true
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                    case 3:
                        btnCancel.userInteractionEnabled = true
                        btnCancel.alpha = 1
                        if dealer_is_update == "1"{
                            btnChangeTime.userInteractionEnabled = true
                            btnChangeTime.alpha = 1
                        }else{
                            btnChangeTime.userInteractionEnabled = false
                            btnChangeTime.hidden = true
                            btnChangeTime.alpha = 0.4
                        }
                        btnCancel.hidden = true
                        btnChangeTime.hidden = true
                        if orderType == "1"{
                            btnReset.setTitle("开始服务", forState: .Normal)
                            btnReset.hidden = false
                            vFooterBG.hidden = false
                            btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                            tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                        }else{
                            btnReset.setTitle("派取车员", forState: .Normal)
                            btnReset.hidden = false
                            vFooterBG.hidden = false
                            btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                            tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                        }
                        
                    case 4://服务已完成，待付款
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                    
                        let backType = self.OrdersInfo?.objectForKey("back_type") as? String!
                        if backType == "1" {//(orderType == "1") || (backType == "1")
                            btnReset.setTitle("已付款,待评价", forState: .Normal)
                            btnReset.hidden = true
                            btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                            vFooterBG.hidden = false
                            tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                            
                        }else if (backType == "2"){
//                            btnReset.setTitle("选择还车员", forState: .Normal)
//                            btnReset.hidden = false
//                            btnReset.frame = CGRectMake(480, 17.5, 125, 35)
//                            vFooterBG.hidden = false
//                            tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30)
                            btnReset.setTitle("已付款,待评价", forState: .Normal)
                            btnReset.hidden = true
                            btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                            vFooterBG.hidden = false
                            tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                        }
                        
                    case 5:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        btnReset.setTitle("变更订单", forState: .Normal)
                        btnReset.hidden = true
                        vFooterBG.hidden = false
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                    case 6:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        btnReset.setTitle("车已到店", forState: .Normal)
                        btnReset.hidden =  false
                        vFooterBG.hidden = false
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                    case 7://已取车
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        btnReset.setTitle("车已到店", forState: .Normal)
                        btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                        btnReset.hidden = false
                        vFooterBG.hidden = false
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                    case 8:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        if orderType == "2"{
                            btnReset.hidden = true
                            btnReset.setTitle("服务完成", forState: .Normal)
                        }else{
                            btnReset.hidden = true
//                            successView.hidden = false
//                            successView.text = "订单待支付"
                            btnReset.setTitle("服务完成，待付款", forState: .Normal)
                        }
                        vFooterBG.hidden = false
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                        btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                    case 9:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        btnReset.setTitle("开始服务", forState: .Normal)
                        let backType = self.OrdersInfo?.objectForKey("back_type") as? String!
                        if orderType == "2"{
                            btnReset.hidden = true
                            btnReset.setTitle("服务完成，待选择还车方式", forState: .Normal)
                            vFooterBG.hidden = false
                            btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                            tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30)
                            
                        }else if backType == "1"{
//                            btnReset.hidden = false
//                            btnReset.setTitle("确认付款", forState: .Normal)
//                            vFooterBG.hidden = false
//                            btnReset.frame = CGRectMake(480, 17.5, 125, 35)
//                            tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30)
                            successView.hidden = false
                            btnReset.hidden = true
                            btnReset.setTitle("服务完成，待选择还车方式", forState: .Normal)
                            vFooterBG.hidden = false
                            btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                            tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30)
                        }else{
                            btnCancel.userInteractionEnabled = false
                            btnCancel.hidden = true
                            btnCancel.alpha = 0.4
                            btnReset.setTitle("", forState: .Normal)
                            btnReset.hidden = true
                            vFooterBG.hidden = false
                            tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30)
                        }
                        
                        
                    case 10:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        
                        btnReset.hidden = false
                        btnReset.setTitle("确认付款", forState: .Normal)
                        vFooterBG.hidden = false
                        btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                        
                        
                    case 11:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        btnReset.setTitle("变更订单", forState: .Normal)
                        btnReset.hidden = true
                        vFooterBG.hidden = false
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                        
                    case 12:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
//                        btnReset.setTitle("变更订单", forState: .Normal)
//                        btnReset.hidden = true
//                        vFooterBG.hidden = false
//                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30)
                        btnReset.setTitle("派送车员", forState: .Normal)
                        btnReset.hidden = false
                        btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                        vFooterBG.hidden = false
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                    case 13:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        btnReset.setTitle("变更订单", forState: .Normal)
                        btnReset.hidden = true
                        cancelImageView.hidden = false
                        vFooterBG.hidden = true
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                    case 14:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        btnReset.setTitle("车已到店", forState: .Normal)
                        btnReset.hidden = false
                        btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                        vFooterBG.hidden = false
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                    case 15:
                        btnCancel.userInteractionEnabled = true
                        btnCancel.alpha = 1
                        if dealer_is_update == "1"{
                            btnChangeTime.userInteractionEnabled = true
                            btnChangeTime.alpha = 1
                        }else{
                            btnChangeTime.userInteractionEnabled = false
                            btnChangeTime.hidden = true
                            btnChangeTime.alpha = 0.4
                        }
                        if orderType == "1"{
                            btnReset.setTitle("开始服务", forState: .Normal)
                            btnReset.hidden = false
                            vFooterBG.hidden = false
                            btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                            tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                        }else{
                            btnReset.setTitle("派取车员", forState: .Normal)
                            btnReset.hidden = false
                            vFooterBG.hidden = false
                            btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                            tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                        }
                    case 16:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        
                        btnReset.hidden = false
                        btnReset.setTitle("确认付款", forState: .Normal)
                        vFooterBG.hidden = false
                        btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                    case 17:
                        btnCancel.userInteractionEnabled = false
                        btnCancel.hidden = true
                        btnCancel.alpha = 0.4
                        btnChangeTime.userInteractionEnabled = false
                        btnChangeTime.hidden = true
                        btnChangeTime.alpha = 0.4
                        successView.text = ""
                        btnReset.setTitle("派送车员", forState: .Normal)
                        btnReset.hidden = true
                        btnReset.frame = CGRectMake(480, 17.5, 125, 35)
                        vFooterBG.hidden = false
                        tvList.frame = CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20)
                    default:
                        break
                        
                    }
                    tvList.reloadData()
//                    self.getNewOrderDetail()

            }
            
            if btnChangeTime.hidden && btnCancel.hidden && btnReset.hidden {
                successView.hidden = false
            } else {
                successView.hidden = true
            }

            if let audio = OrdersInfo?.objectForKey("audio") as! String!{
                SWUtils.RunOnBackgroundThread({ () -> Void in
                    autoreleasepool({ () -> () in
                        let filename = audio.lastPathComponent
                        var data: NSData?
                        if NSFileManager.defaultManager().fileExistsAtPath(filename.temporaryPath()){
                            data = NSData(contentsOfFile: filename.temporaryPath())
                        }else{
                            data = NSData(contentsOfURL: NSURL(string: audio)!)
                            data?.writeToFile(filename.temporaryPath(), atomically: false)
                        }
                        
                        if data != nil{
                            if self.player != nil{
                                self.player!.stop()
                            }
                            
                            self.player = try? AVAudioPlayer(data: data!)
                            self.player?.delegate = self
                            self.player?.prepareToPlay()
                            SWUtils.RunOnMainThread({ () -> Void in
                                self.tvList.reloadData()
                            })
                        }
                        
                    })
                })
                
            }else{
                if self.player != nil{
                    self.player!.stop()
                }
                
                self.player = nil
                
            }
            
            btnPlay?.selected = false
            tvList.reloadData()
            updateAllOrdersInfo()

        }
    }
    
     var ordersListDelegate: SHOrdersListDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(red: 0.96, green: 0.96, blue: 0.97, alpha: 1)
        
        
        tvList = UITableView(frame: CGRectMake(15, 15, frame.size.width-30, frame.size.height-64-49-30-20))
        tvList.delegate = self
        tvList.dataSource = self
        tvList.backgroundColor = UIColor.whiteColor()
        tvList.separatorStyle = .None
        tvList.registerClass(UITableViewCell.self, forCellReuseIdentifier: "identifierDetail")
        tvList.layer.borderColor = UIColor.init(red: 222/255.0, green: 222/255.0, blue: 224/255.0, alpha: 1).CGColor
        tvList.layer.borderWidth = 1
        self.addSubview(tvList)
        
        cancelImageView = UIImageView.init(frame: CGRect.init(x: CGRectGetMaxX(tvList.frame)-209, y: CGRectGetMaxY(tvList.frame)-160, width: 209, height: 160))
        cancelImageView.image = UIImage.init(named: "cancelWatermarkBig")
        cancelImageView.hidden = true
        self.addSubview(cancelImageView)
        
//        nothingView = UIView.init(frame: CGRectMake(0, 0, tvList.frame.size.width, tvList.frame.size.height))
//        nothingView.backgroundColor = UIColor.init(red: 242/255.0, green: 242/255.0, blue: 242/255.0, alpha: 1)
//        tvList.addSubview(nothingView)
//        
//        var imageView = UIImageView.init(frame: CGRectMake(, <#T##y: CGFloat##CGFloat#>, 438, 306))
        
        refreshController = UIRefreshControl()
        refreshController.addTarget(self, action: #selector(SHOrdersListView.refreshChanged(_:)), forControlEvents: .ValueChanged)
        tvList.addSubview(refreshController)
        
        //        var vLine3 = UIView (frame: CGRectMake(0 , 620,  frame.size.width, 1))
        //        vLine3.backgroundColor = UIColor(white: 0.90, alpha: 1)
        //        vDetail.addSubview(vLine3)
        
        
        vFooterBG = UIView.init(frame: CGRectMake(15, frame.size.height-70-64-20, frame.size.width-30, 70))
        vFooterBG.backgroundColor = UIColor(red: 247/255.0, green: 247/255.0, blue: 247/255.0, alpha: 1)
        vFooterBG.layer.shadowColor = UIColor(red: 222/255.0, green: 222/255.0, blue: 225/255.0, alpha: 1).CGColor
//        vFooterBG.layer.shadowRadius = 1
//        vFooterBG.layer.shadowOffset = CGSizeMake(-2, 0)
//        vFooterBG.layer.shadowOpacity = 0.1

        vFooterBG.layer.borderWidth = 1
        vFooterBG.layer.borderColor = UIColor.init(red: 222/255.0, green: 222/255.0, blue: 224/255.0, alpha: 1).CGColor
        vFooterBG.hidden = false
        self.addSubview(vFooterBG)
        
        lblMoneyCountTitle = UILabel.create(CGRectMake(15,25,60,20), font: UIFont.systemFontOfSize(15), textColor: UIColor.blackColor())
        lblMoneyCountTitle.text = "合计金额"
//        vFooterBG.addSubview(lblMoneyCountTitle)
        
        lblMoneyCount = UILabel.create(CGRectMake(85,25,120,20), font: UIFont.systemFontOfSize(14), textColor: UIColor.redColor())
//        lblMoneyCount.text = "￥344.00"
//        vFooterBG.addSubview(lblMoneyCount)
        
        btnCancel = UIButton(type: .Custom)
        btnCancel.frame = CGRectMake(210, 17.5, 125, 35)
        btnCancel.setBackgroundImage(UIImage(named: "button_normal.png"), forState:.Normal)
        btnCancel.setTitle("取消订单", forState: .Normal)
        btnCancel.titleLabel?.font = UIFont.systemFontOfSize(12)
        btnCancel.setTitleColor(UIColor.blackColor(), forState:.Normal)
        btnCancel.addTarget(self, action: #selector(SHOrdersListView.startButtonClicked(_:)), forControlEvents: .TouchUpInside)

        vFooterBG.addSubview(btnCancel)
        btnCancel.tag = 1010
        
        
        btnChangeTime = UIButton(type: .Custom)
        btnChangeTime.frame = CGRectMake(345, 17.5, 125, 35)
        btnChangeTime.setBackgroundImage(UIImage(named: "button_normal.png"), forState:.Normal)
        btnChangeTime.setTitle("变更时间", forState: .Normal)
        btnChangeTime.titleLabel?.font = UIFont.systemFontOfSize(12)
        btnChangeTime.setTitleColor(UIColor.blackColor(), forState:.Normal)
        btnChangeTime.addTarget(self, action: #selector(SHOrdersListView.startButtonClicked(_:)), forControlEvents: .TouchUpInside)
        vFooterBG.addSubview(btnChangeTime)
        btnChangeTime.tag = 1012
        
        btnReset = UIButton(type: .Custom)
        btnReset.frame = CGRectMake(480, 17.5, 125, 35)
        btnReset.setBackgroundImage(UIImage(named: "button_selected.png"), forState:.Normal)
        btnReset.setTitle("车已到店", forState: .Normal)
        btnReset.titleLabel?.font = UIFont.systemFontOfSize(12)
        btnReset.setTitleColor(UIColor.whiteColor(), forState:.Normal)
        btnReset.addTarget(self, action: #selector(SHOrdersListView.startButtonClicked(_:)), forControlEvents: .TouchUpInside)
        btnReset.clipsToBounds = true

        vFooterBG.addSubview(btnReset)
        btnReset.tag = 1011
        self.refreshData()
        
        successView = UILabel.init(frame: CGRectMake(0, 0, tvList.frame.size.width, 70))
        successView.font = UIFont.systemFontOfSize(20)
        successView.textAlignment = .Center
        successView.textColor = UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1)
        successView.hidden = true
        vFooterBG.addSubview(successView)
    }
    
    func creatQRForString(qrString:String?,qrImageName:String?) ->UIImage?{
        if let sureQRString = qrString {
            let stringData = sureQRString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
            // 创建一个二维码的滤镜
            let qrFilter = CIFilter(name: "CIQRCodeGenerator")
            qrFilter!.setValue(stringData, forKey: "inputMessage")
            qrFilter!.setValue("H", forKey: "inputCorrectionLevel")
            let qrCIImage = qrFilter!.outputImage
            // 创建一个颜色滤镜,黑白色
            let colorFilter = CIFilter(name: "CIFalseColor")
            colorFilter!.setDefaults()
            colorFilter!.setValue(qrCIImage, forKey: "inputImage")
            colorFilter!.setValue(CIColor(red: 0, green: 0, blue: 0), forKey: "inputColor0")
            colorFilter!.setValue(CIColor(red: 1, green: 1, blue: 1), forKey: "inputColor1")
            // 返回二维码image
            let codeImage = UIImage(CIImage: colorFilter!.outputImage!.imageByApplyingTransform(CGAffineTransformMakeScale(5, 5)))
            // 通常,二维码都是定制的,中间都会放想要表达意思的图片
            if let iconImage = UIImage(named: qrImageName!) {
                let rect = CGRectMake(0, 0, codeImage.size.width, codeImage.size.height)
                UIGraphicsBeginImageContext(rect.size)
                
                codeImage.drawInRect(rect)
                let avatarSize = CGSizeMake(rect.size.width * 0.25, rect.size.height * 0.25)
                let x = (rect.width - avatarSize.width) * 0.5
                let y = (rect.height - avatarSize.height) * 0.5
                iconImage.drawInRect(CGRectMake(x, y, avatarSize.width, avatarSize.height))
                let resultImage = UIGraphicsGetImageFromCurrentImageContext()
                
                UIGraphicsEndImageContext()
                return resultImage
            }
            return codeImage
        }
        return nil
    }
    
    func refreshData(){
        ordersListDelegate?.allOrdesUpdated(OrdersInfo)
    }
    
    func startButtonClicked(sender:UIButton){
        sender.userInteractionEnabled = false
        NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: #selector(SHOrdersListView.confirmClicked(_:)), object: sender)
        self.performSelector(#selector(SHOrdersListView.confirmClicked(_:)), withObject: sender, afterDelay: 1)
    }
    
    func confirmClicked(sender:UIButton){
        switch sender.tag{
        case 100:
            print("100")
            ordersListDelegate?.thePreviousStepClick(indexNumber)
            
        case 101:
            print("101")
            ordersListDelegate?.theNectStepClick(indexNumber)
        case 102:
            self.showActionSheet(sender)
            //            self.adjustClicked(sender)
            //            allOrdersDelegate?.showSelectedTakeCarPerson()
        case 1000:
            print("1000")
            ordersListDelegate?.hiddenTakeCarPerson()
        case 1002:
            print("1002")
            ordersListDelegate?.showSelectedTakeCarPerson()
        case 103:
            print("103")
            strShowType = "1"
            tvList.reloadData()
        case 104:
            print("104")
            strShowType = "1"
            tvList.reloadData()
        case 1010://取消订单
            sender.userInteractionEnabled = false
            self.cancleOrder()
        case 1012://变更时间
            sender.userInteractionEnabled = true
            self.adjustClicked()
        case 1011:
            sender.userInteractionEnabled = false
            print("1011")
            let orderStatus =  strOrderStatus.intValue
            let orderType = self.OrdersInfo?.objectForKey("pick_type") as! String!
            
            switch orderStatus{
            case 1:
                self.confirmClicked()
//                ordersListDelegate?.showSelectedTakeCarPerson()
                //                self.confirmClicked()
            case 2: break
                //                self.adjustClicked(sender)
            case 3:
                
                if orderType == "2"{
                    ordersListDelegate?.showSelectedTakeCarPerson()
                }else{
                    self.startService()
                }
                
                //                allOrdersDelegate?.hideOrderDetailView(indexNumber)
            case 4:break
                

                
                
            case 5:break
                
            case 6:break
                
            case 7://已取车
                self.startService()
            case 8://开始服务
                sender.userInteractionEnabled = true
                if orderType == "2"{
                    //                    self.endService()
                    
                    ordersListDelegate?.hiddenTakeCarPerson()
                }else{
                    //                     self.endService()
                    ordersListDelegate?.hiddenTakeCarPerson()
                }
                
            case 9:break
                
//                if sender.titleLabel?.text == "确认付款"{
//                    self.alreadyPay()
//                }
//                 ordersListDelegate?.showSelectedTakeCarPerson()//选取还车员
            case 10:
                //                allOrdersDelegate?.showSelectedTakeCarPerson()
                //                allOrdersDelegate?.hideOrderDetailView(indexNumber)
                if sender.titleLabel?.text == "确认付款"{
                    let alert = UIAlertView(title: nil, message: "您是否要确认付款？", delegate: self, cancelButtonTitle: "取消")
                    alert.addButtonWithTitle("确认付款")
                    alert.tag = 1000
                    alert.show()
//                    self.alreadyPay()
                }
            case 11:break
            case 12:
                 sender.userInteractionEnabled = true
                 ordersListDelegate?.showSelectedTakeCarPerson()
            case 13:break
            case 14:
                self.confirmClicked()
            case 15:
                if orderType == "2"{
                     sender.userInteractionEnabled = true
                    ordersListDelegate?.showSelectedTakeCarPerson()
                }else{
                    self.startService()
                }
            case 16:
                if sender.titleLabel?.text == "确认付款"{
//                    self.alreadyPay()
                    let alert = UIAlertView(title: nil, message: "您是否要确认付款？", delegate: self, cancelButtonTitle: "取消")
                    alert.addButtonWithTitle("确定付款")
                    alert.tag = 1000
                    alert.show()
                }
            case 17:
                ordersListDelegate?.showSelectedTakeCarPerson()
                
            default:
                break
                
            }

            
            
        default:()
            
        }
    }
    
    func refreshChanged(sender: UIRefreshControl){
        self.refresh()
    }
    
    func refresh(){
        NSThread.detachNewThreadSelector(#selector(SHOrdersListView.getNewOrderDetail), toTarget: self, withObject: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UIActionSheet Delegate
    func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
        
        if buttonIndex >= 0 {
            let buttonTitle = actionSheet.buttonTitleAtIndex(buttonIndex)
            if buttonTitle=="变更订单"{
                self.adjustClicked()
            }else if buttonTitle == "取消订单"{
                self.cancleOrder()
            }else{
                return
            }
            
        }
        
    }
    
    func showActionSheet(sender:UIButton){
        
        let aryStatusDetail = self.OrdersInfo?.objectForKey("status_detail")
        let aryActionButtons = NSMutableArray()
        let status = NSMutableArray()
        
        for i in 0 ..< aryStatusDetail!.count{
            let dic = aryStatusDetail?.objectAtIndex(i) as! NSDictionary
            let orderStatus = dic.objectForKey("status")
            status.addObject(orderStatus!)
        }
        
        if status.indexOfObject("14") == NSNotFound{
            let index = aryActionButtons.indexOfObject("变更订单")
            if index == NSNotFound {
                aryActionButtons.addObject("变更订单")
            }
        }
        
        if status.indexOfObject("13") == NSNotFound{
            let index = aryActionButtons.indexOfObject("取消订单")
            if index == NSNotFound {
                aryActionButtons.addObject("取消订单")
            }
        }
        
        if aryActionButtons.count > 0 {
            let actionSheet = UIActionSheet()
            actionSheet.title = "选择要进行的操作"
            actionSheet.delegate = self
            for j in 0 ..< aryActionButtons.count{
                [actionSheet .addButtonWithTitle(aryActionButtons.objectAtIndex(j) as? String)]
            }
            actionSheet.addButtonWithTitle("取消")
            actionSheet.showFromRect(sender.frame, inView: self, animated: true)
        }
        
        
    }
    
    
    func updateButtonStatus(){
        
        self.tvList.reloadData()
        ordersListDelegate?.allOrdesUpdated(OrdersInfo)
    }
    
    //获取最新订单详情
    func getNewOrderDetail(){
        SWUtils.RunOnBackgroundThread { () -> Void in
            autoreleasepool({ () -> () in
                if let order_id = self.OrdersInfo?.objectForKey("order_id") as! String!{
                    
                    
                    let dict = SHDataProvider.getOrderDetail(["order_id":order_id,"account_type":"1"])
//                    print(dict)
                    SWUtils.RunOnMainThread({ () -> Void in
                        if let code = dict?.objectForKey("code")?.intValue{
                            if code==200{
                                self.OrdersInfo = NSDictionary(dictionary: (dict?.objectForKey("data"))! as! [NSObject : AnyObject])
                                //\\
//                               var OrderStatus = self.OrdersInfo?.objectForKey("order_status") as! String
//                                if self.strOrderStatus != OrderStatus{
                                    self.updateAllOrdersInfo()
                                   
//                                }
                                
                                
                            }else{

                            }
                        }else{

                        }
                        
                         self.refreshController.endRefreshing()

                    })
                }
            })
        }
        
    }
    
    //确认
    func confirmClicked(){
        SWUtils.RunOnBackgroundThread { () -> Void in
            autoreleasepool({ () -> () in
                if let order_id = self.OrdersInfo?.objectForKey("order_id") as! String!{
                    let dict = SHDataProvider.confimOrder(NSDictionary(object: order_id,forKey: "order_id"))
                    
                    SWUtils.RunOnMainThread({ () -> Void in
                        if let code = dict?.objectForKey("code")?.intValue{
                            if code==200{
                                UIAlertView.show(nil, message: "操作成功", cancelButton: nil)
//                                var mut = NSMutableDictionary(dictionary: self.OrdersInfo!) as NSMutableDictionary
//                                mut.setObject("3", forKey: "order_status")
//                                
//                                self.OrdersInfo = NSDictionary(dictionary: mut)
                                self.OrdersInfo = NSDictionary(dictionary: (dict?.objectForKey("data"))! as! [NSObject : AnyObject])
                                self.updateAllOrdersInfo()
                                
                            }else{
                                if let msg = dict?.objectForKey("message") as? String{
                                    UIAlertView.show(nil, message: msg, cancelButton: nil)
                                }else{
                                    UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                                }
                            }
                        }else{
                            UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                        }
                        
//                        self.getNewOrderDetail()
                    })
                }
            })
        }
    }
    
    //取消订单
    func cancleOrder(){
        SWUtils.RunOnBackgroundThread { () -> Void in
            autoreleasepool({ () -> () in
                if let order_id = self.OrdersInfo?.objectForKey("order_id") as! String!{
                    
                    let dict = SHDataProvider.cancleOrder(["order_id":order_id,"dealer_id":SHDataProvider.getDealerID()])
                    
                    SWUtils.RunOnMainThread({ () -> Void in
                        if let code = dict?.objectForKey("code")?.intValue{
                            if code==200{
                                UIAlertView.show(nil, message: "操作成功", cancelButton: nil)
                                let mut = NSMutableDictionary(dictionary: self.OrdersInfo!) as NSMutableDictionary
                                mut.setObject("13", forKey: "order_status")
                                
//                                self.OrdersInfo = NSDictionary(dictionary: mut)
                                self.OrdersInfo = NSDictionary(dictionary: (dict?.objectForKey("data"))! as! [NSObject : AnyObject])
                                self.updateAllOrdersInfo()
                            }else{
                                if let msg = dict?.objectForKey("message") as? String{
                                    UIAlertView.show(nil, message: msg, cancelButton: nil)
                                }else{
                                    UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                                }
                            }
                        }else{
                            UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                        }
                        
//                        self.getNewOrderDetail()
                        
                        
                    })
                }
            })
        }
    }
    
    //开始服务
    func startService(){
        SWUtils.RunOnBackgroundThread { () -> Void in
            autoreleasepool({ () -> () in
                if let order_id = self.OrdersInfo?.objectForKey("order_id") as! String!{
                    let dict = SHDataProvider.startService(NSDictionary(object: order_id,forKey: "order_id"))
                    
                    SWUtils.RunOnMainThread({ () -> Void in
                        if let code = dict?.objectForKey("code")?.intValue{
                            if code==200{
                                UIAlertView.show(nil, message: "操作成功", cancelButton: nil)
                                let mut = NSMutableDictionary(dictionary: self.OrdersInfo!) as NSMutableDictionary
                                mut.setObject("8", forKey: "order_status")
                                
//                                self.OrdersInfo = NSDictionary(dictionary: mut)
                                self.OrdersInfo = NSDictionary(dictionary: (dict?.objectForKey("data"))! as! [NSObject : AnyObject])
                                self.updateAllOrdersInfo()
                            }else{
                                if let msg = dict?.objectForKey("message") as? String{
                                    UIAlertView.show(nil, message: msg, cancelButton: nil)
                                }else{
                                    UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                                }
                            }
                        }else{
                            UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                        }
                        
//                        self.getNewOrderDetail()
                        
                        
                    })
                }
            })
        }
    }
    
    
    //服务完成
    func endService(){
        SWUtils.RunOnBackgroundThread { () -> Void in
            autoreleasepool({ () -> () in
                if let order_id = self.OrdersInfo?.objectForKey("order_id") as! String!{
                    let dict = SHDataProvider.endService(["order_id":order_id,"price":"1000"])
                    
                    SWUtils.RunOnMainThread({ () -> Void in
                        if let code = dict?.objectForKey("code")?.intValue{
                            if code==200{
                                UIAlertView.show(nil, message: "操作成功", cancelButton: nil)
                                let mut = NSMutableDictionary(dictionary: self.OrdersInfo!) as NSMutableDictionary
                                mut.setObject("4", forKey: "order_status")
                                
//                                self.OrdersInfo = NSDictionary(dictionary: mut)
                                self.OrdersInfo = NSDictionary(dictionary: (dict?.objectForKey("data"))! as! [NSObject : AnyObject])
                                self.updateAllOrdersInfo()
                            }else{
                                if let msg = dict?.objectForKey("message") as? String{
                                    UIAlertView.show(nil, message: msg, cancelButton: nil)
                                }else{
                                    UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                                }
                            }
                        }else{
                            UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                        }
                        
//                        self.getNewOrderDetail()
                    })
                }
            })
        }
    }
    
    
    //确认付款
    func alreadyPay(){
        SWUtils.RunOnBackgroundThread { () -> Void in
            autoreleasepool({ () -> () in
                if let order_id = self.OrdersInfo?.objectForKey("order_id") as! String!{
                    let dict = SHDataProvider.alreadyPay(NSDictionary(object: order_id,forKey: "order_id"))
                    
                    SWUtils.RunOnMainThread({ () -> Void in
                        if let code = dict?.objectForKey("code")?.intValue{
                            if code==200{
                                UIAlertView.show(nil, message: "操作成功", cancelButton: nil)
                                let mut = NSMutableDictionary(dictionary: self.OrdersInfo!) as NSMutableDictionary
                                mut.setObject("12", forKey: "order_status")
                                
//                                self.OrdersInfo = NSDictionary(dictionary: mut)
                                self.OrdersInfo = NSDictionary(dictionary: (dict?.objectForKey("data"))! as! [NSObject : AnyObject])
                                self.updateAllOrdersInfo()
                            }else{
                                if let msg = dict?.objectForKey("message") as? String{
                                    UIAlertView.show(nil, message: msg, cancelButton: nil)
                                }else{
                                    UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                                }
                            }
                        }else{
                            UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                        }
                        
//                        self.getNewOrderDetail()
                        
                        
                    })
                }
            })
        }
    }
   
    func btnClick() {
        let picker: UIDatePicker = pop.contentViewController.view.viewWithTag(100) as! UIDatePicker
        btnChangeTime.userInteractionEnabled = true
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        if let orderTime = OrdersInfo?.objectForKey("order_time") as? String{
            let date = formatter.dateFromString(orderTime)
            formatter.dateFormat = "yyyy-MM-dd HH:mm"
            
            if formatter.stringFromDate(picker.date) != formatter.stringFromDate(date!){
                selectedDate = picker.date
                pop.dismissPopoverAnimated(true)
                let alert = UIAlertView(title: nil, message: "是否确定要变更预约时间？", delegate: self, cancelButtonTitle: "取消")
                alert.addButtonWithTitle("确定")
                alert.tag = 100
                alert.show()
            }
        }
    }
    
    func btnCancleClick() {
        pop.dismissPopoverAnimated(true)
    }
    
    func adjustClicked(){
        let vc = UIViewController()
        let picker = UIDatePicker()
        
        picker.frame = CGRectMake(0, 30, 320, 218)
        picker.minuteInterval = 30
        
        picker.datePickerMode = UIDatePickerMode.DateAndTime
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        
        let minday = formatter.dateFromString(formatter.stringFromDate(NSDate().dateByAddingTimeInterval(24*3600)))
        let maxday = formatter.dateFromString(formatter.stringFromDate(NSDate().dateByAddingTimeInterval(24*3600*3)))
        
        picker.minimumDate = minday?.dateByAddingTimeInterval(9*3600)
        picker.maximumDate = maxday?.dateByAddingTimeInterval(17*3600)
        picker.tag = 100
        picker.addTarget(self, action: #selector(SHOrdersListView.dateChanged(_:)), forControlEvents: .ValueChanged)
        vc.view.addSubview(picker)
        
        let titleLabel = UILabel.init(frame: CGRectMake(0, 10, 320, 20))
        titleLabel.text = "选择时间"
        titleLabel.font = UIFont.systemFontOfSize(16)
        titleLabel.textColor = UIColor.init(red: 74/255.0, green: 74/255.0, blue: 74/255.0, alpha: 1)
        titleLabel.textAlignment = .Center
        vc.view.addSubview(titleLabel)
        
        let cancleBtn = UIButton.init(type: .Custom)
        cancleBtn.frame = CGRectMake(10, 10, 40, 20)
        cancleBtn.setTitle("取消", forState: .Normal)
        cancleBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        cancleBtn.setTitleColor(UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1), forState: .Normal)
        cancleBtn.addTarget(self, action: #selector(SHOrdersListView.btnCancleClick), forControlEvents: .TouchUpInside)
        vc.view.addSubview(cancleBtn)
        
        let okBtn = UIButton.init(type: .Custom)
        okBtn.frame = CGRectMake(320-50, 10, 40, 20)
        okBtn.setTitle("确定", forState: .Normal)
        okBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        okBtn.setTitleColor(ORANGECOLOR, forState: .Normal)
        okBtn.addTarget(self, action: #selector(SHOrdersListView.btnClick), forControlEvents: .TouchUpInside)
        vc.view.addSubview(okBtn)
        
        let lineLabel = UILabel.init(frame: CGRectMake(15, 30, 320-30, 1))
        lineLabel.backgroundColor = UIColor.blackColor()
        vc.view.addSubview(lineLabel)
        
        
//         var btnDoneChangeTime = UIButton(type: .Custom)
//        btnDoneChangeTime.frame = CGRectMake(0, 218,320, 35)
//        btnDoneChangeTime.setBackgroundImage(UIImage(named: "button_selected.png"), forState:.Normal)
//        btnDoneChangeTime.setTitle("确认", forState: .Normal)
//        btnDoneChangeTime.titleLabel?.font = UIFont.systemFontOfSize(12)
//        btnDoneChangeTime.setTitleColor(UIColor.whiteColor(), forState:.Normal)
//        btnDoneChangeTime.addTarget(self, action: "startButtonClicked:", forControlEvents: .TouchUpInside)
//        btnDoneChangeTime.clipsToBounds = true
//        vc.view.addSubview(btnDoneChangeTime)
       

        pop = UIPopoverController(contentViewController: vc)
        pop.delegate = self
        pop.popoverContentSize = CGSizeMake(320, 218+35)
        pop.presentPopoverFromRect(CGRectMake(352,100,320,218), inView: self, permittedArrowDirections: .Up, animated: true)
        
        
    }
    
    func dateChanged(sender: UIDatePicker){
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd";
        
        
        let baseDate = formatter.dateFromString(formatter.stringFromDate(sender.date)) as NSDate!
        
        if sender.date.timeIntervalSinceDate(baseDate.dateByAddingTimeInterval(9*3600)) >= 0 && baseDate.dateByAddingTimeInterval(17*3600).timeIntervalSinceDate(sender.date) >= 0{
            
        }else if sender.date.timeIntervalSinceDate(baseDate.dateByAddingTimeInterval(9*3600)) < 0{
            sender.date = baseDate.dateByAddingTimeInterval(9*3600)
        }else{
            sender.date = baseDate.dateByAddingTimeInterval(17*3600)
        }
        
    }

    //  MARK: - UITableView Delegate & Data Source
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
//        let identifierDetail = "OrderDetail"
//        let identifierStatus = "OrderStatus"
        
//        if strShowType == "1"{
//        var cell = tableView.dequeueReusableCellWithIdentifier(identifierDetail, forIndexPath: indexPath) as UITableViewCell!
//            var cell = tableView.dequeueReusableCellWithIdentifier(identifierDetail) as UITableViewCell!
//            if cell == nil {
//                cell = UITableViewCell(style: .Default, reuseIdentifier: identifierDetail)
//                var lbl = UILabel.create(CGRectMake(10, 0, 80, 44), font: UIFont.systemFontOfSize(16), textColor: UIColor(white: 0.5, alpha: 1))
//                lbl.tag = 100
//                cell.contentView.addSubview(lbl)
//                
//                lbl = UILabel.create(CGRectMake(95, 0,575 , 44), font: UIFont.systemFontOfSize(16), textColor: UIColor.blackColor())
//                lbl.tag = 101
//                cell.contentView.addSubview(lbl)
                 let cell = tableView .dequeueReusableCellWithIdentifier("identifierDetail", forIndexPath: indexPath) as UITableViewCell
        for view in cell.contentView.subviews{
            if view.isKindOfClass(UIView){
                view.removeFromSuperview()
            }
        }
        
        
        if let order_status = OrdersInfo?.objectForKey("order_status")?.integerValue as Int!{
            
            let processDic = OrdersInfo?.objectForKey("process")
            
            if order_status == 1 || order_status == 3 || order_status == 13 || processDic == nil{
                successView.hidden = true
            } else {
                successView.hidden = false
                if indexPath.row == 0 {
                    let titles = ["预约人","预约时间","联系电话","服务方式"]
                    for i in 0 ..< titles.count{
                        let col = i % 4
                        //                        let row = i / 2
                        
                        var margin:Int = 0
                        
                        if i > 1 {
                            margin = 100
                        }
                        
                        var lbl = UILabel.create(CGRectMake(CGFloat(15+140*col+margin), CGFloat(15), 100, 13), font: UIFont.systemFontOfSize(12), textColor:UIColor.grayColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                        //                        lbl.text = titles[i]
                        lbl.tag = 20000+i
                        cell.contentView.addSubview(lbl)
                        
                        lbl = UILabel.create(CGRectMake(CGFloat(15+140*col+margin), CGFloat(35), 100, 18), font: UIFont.systemFontOfSize(14), textColor: UIColor.blackColor())
                        //            lbl.text = titles[i]
                        lbl.tag = 30000+i
                        if i == 1{
                            lbl.frame = CGRectMake(CGFloat(15+140*col), 35, 200, 15)
                        }
                        cell.contentView.addSubview(lbl)
                    }
                } else if indexPath.row == 1 {
                    let lbl = UILabel.create(CGRectMake(15, 15, 100, 13), font: UIFont.systemFontOfSize(12), textColor: UIColor.grayColor())
                    lbl.text = "服务进度"
                    lbl.textAlignment = .Left
                    cell.contentView.addSubview(lbl)
                    
                    let imageView = UIImageView.init(frame: CGRectMake(15, 35, tvList.frame.size.width-30, 80))
                    imageView.tag = 1002
                    imageView.contentMode = .ScaleAspectFit
                    cell.contentView.addSubview(imageView)
                    
                    let priceNumber = UILabel.create(CGRectMake(15, CGRectGetMaxY(imageView.frame)+31, 100, 25), font: UIFont.systemFontOfSize(16), textColor: UIColor.init(red: 56/255.0, green: 56/255.0, blue: 56/255.0, alpha: 1))
                    priceNumber.text = "消费金额："
                    priceNumber.tag = 2100
                    priceNumber.textAlignment = .Left
                    cell.contentView.addSubview(priceNumber)
                    
                    let priceLabel = UILabel.create(CGRectMake(CGRectGetMaxX(priceNumber.frame), CGRectGetMaxY(imageView.frame)+20, 131, 43), font: UIFont.systemFontOfSize(32), textColor: UIColor.init(red: 1, green: 143/255.0, blue: 0, alpha: 1))
                    priceLabel.tag = 1004
                    cell.contentView.addSubview(priceLabel)
                    
                    for i in 0 ..< 6 {
                        let col = i % 3
                        let lin = i / 3
                        let width = (tvList.frame.width - 30)/3
                        
                        let timeLabel = UILabel.create(CGRectMake(CGFloat(15 + CGFloat(col)*width), CGRectGetMaxY(priceNumber.frame)+10+CGFloat(30*lin), width, 28), font: UIFont.systemFontOfSize(14), textColor: UIColor.init(red: 74/255.0, green: 74/255.0, blue: 74/255.0, alpha: 1))
                        timeLabel.tag = 1005+i
                        cell.contentView.addSubview(timeLabel)
                    }
                    
                } else if indexPath.row == 2 {
                    var lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(15), 100, 13), font: UIFont.systemFontOfSize(12), textColor:UIColor.grayColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                    //                    lbl.text = "还车地址"
                    lbl.tag = 410
                    cell.contentView.addSubview(lbl)
                    
                    var imgv = UIImageView.init(frame: CGRectMake(15, 35, tvList.frame.size.width-30, 38))
                    imgv.image = UIImage(named: "retrun_car_addres_map.png")
                    cell.contentView.addSubview(imgv)
                    
                    imgv = UIImageView.init(frame: CGRectMake(25, 40, 20, 27))
                    imgv.image = UIImage(named: "location.png")
                    cell.contentView.addSubview(imgv)
                    
                    lbl = UILabel.create(CGRectMake(CGFloat(50), CGFloat(40), 560, 28), font: UIFont.systemFontOfSize(14), textColor:UIColor(red: 74/255.0, green: 74/255.0, blue: 74/255.0, alpha: 1))//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                    lbl.tag = 411
                    cell.contentView.addSubview(lbl)
                    
                } else if indexPath.row == 3 {
                    
                    if carstatus != nil && carstatus < 3{
                        var lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(15), 100, 13), font: UIFont.systemFontOfSize(12), textColor:UIColor.grayColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                        //                    lbl.text = "服务内容"
                        lbl.tag = 420
                        cell.contentView.addSubview(lbl)
                        
                        lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(35), tvList.frame.size.width-30, 15), font: UIFont.systemFontOfSize(14), textColor:UIColor.blackColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                        lbl.tag = 421
                        cell.contentView.addSubview(lbl)
                        
                        let imgv = UIImageView.init(frame: CGRectMake(15, 74, tvList.frame.size.width-30, 1))
                        imgv.image = UIImage(named: "dashe_ line.png")
                        imgv.tag = 422
                        cell.contentView.addSubview(imgv)
                    } else {
                        let partsPriceLabel = UILabel.create(CGRectMake(15, 20, 50, 17), font: UIFont.systemFontOfSize(12), textColor: UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1))
                        partsPriceLabel.text = "配件费"
                        partsPriceLabel.textAlignment = .Left
                        cell.contentView.addSubview(partsPriceLabel)
                        
                        for i in 0 ..< 30 {
                            let nameLabel = UILabel.create(CGRectMake(15, CGRectGetMaxY(partsPriceLabel.frame)+CGFloat(10+i*30), 200, 20), font: UIFont.systemFontOfSize(14), textColor: UIColor.init(red: 74/255.0, green: 74/255.0, blue: 74/255.0, alpha: 1))
                            nameLabel.tag = 1100+i
                            nameLabel.textAlignment = .Left
                            nameLabel.hidden = true
                            cell.contentView.addSubview(nameLabel)
                            
                            let numberLabel = UILabel.create(CGRectMake(CGRectGetMaxX(nameLabel.frame), CGRectGetMinY(nameLabel.frame), 100, 20), font: UIFont.systemFontOfSize(14), textColor: UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1))
                            numberLabel.tag = 1200+i
                            numberLabel.hidden = true
                            numberLabel.textAlignment = .Left
                            cell.contentView.addSubview(numberLabel)
                            
                            let originalPriceLabel = UILabel.create(CGRectMake(0, CGRectGetMinY(numberLabel.frame), tvList.frame.size.width-200, 20), font: UIFont.systemFontOfSize(14), textColor: UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1))
                            originalPriceLabel.tag = 1300+i
                            originalPriceLabel.hidden = true
                            originalPriceLabel.textAlignment = .Right
                            cell.contentView.addSubview(originalPriceLabel)
                            originalPriceLabel.sizeToFit()
                            originalPriceLabel.frame = CGRectMake(tvList.frame.size.width-200-CGRectGetWidth(originalPriceLabel.frame), CGRectGetMinY(numberLabel.frame), CGRectGetWidth(originalPriceLabel.frame), 20)
                            
                            //                        var lineLabel = UILabel.create(CGRectMake(CGRectGetMinX(originalPriceLabel.frame)-2, CGRectGetMidY(originalPriceLabel.frame), CGRectGetWidth(originalPriceLabel.frame)+4, 1), font: UIFont.systemFontOfSize(1), textColor: UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1))
                            let lineLabel = UILabel.init(frame: CGRectMake(CGRectGetMinX(originalPriceLabel.frame)-2, CGRectGetMidY(originalPriceLabel.frame), CGRectGetWidth(originalPriceLabel.frame)+4, 1))
                            lineLabel.backgroundColor = UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1)
                            lineLabel.tag = 1600+i
                            lineLabel.hidden = true
                            cell.contentView.addSubview(lineLabel)
                            
                            let priceLabel = UILabel.create(CGRectMake(0, CGRectGetMinY(numberLabel.frame), tvList.frame.size.width-70, 20), font: UIFont.systemFontOfSize(14), textColor: UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1))
                            priceLabel.tag = 1400+i
                            priceLabel.hidden = true
                            priceLabel.textAlignment = .Right
                            cell.contentView.addSubview(priceLabel)
                        }
                        let workPriceTitle = UILabel.create(CGRectMake(15, CGRectGetMaxY(partsPriceLabel.frame)+CGFloat(10+30*10), 100, 17), font: UIFont.systemFontOfSize(12), textColor: UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1))
                        workPriceTitle.textAlignment = .Left
                        workPriceTitle.tag = 15007
                        workPriceTitle.text = "工时费"
                        cell.contentView.addSubview(workPriceTitle)
                        
                        let workPriceNumber = UILabel.create(CGRectMake(15, CGRectGetMaxY(workPriceTitle.frame)+10, 100, 20), font: UIFont.systemFontOfSize(14), textColor: UIColor.init(red: 74/255.0, green: 74/255.0, blue: 74/255.0, alpha: 1))
                        workPriceNumber.textAlignment = .Left
                        workPriceNumber.tag = 15008
                        workPriceNumber.text = "工时费"
                        cell.contentView.addSubview(workPriceNumber)
                        
                        let workPriceLabel = UILabel.create(CGRectMake(0, CGRectGetMinY(workPriceNumber.frame), tvList.frame.size.width-70, 20), font: UIFont.systemFontOfSize(14), textColor: UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1))
                        workPriceLabel.textAlignment = .Right
                        workPriceLabel.tag = 15001
                        cell.contentView.addSubview(workPriceLabel)
                        
                        let otherTitle = UILabel.create(CGRectMake(15, CGRectGetMaxY(workPriceNumber.frame)+10, 100, 20), font: UIFont.systemFontOfSize(12), textColor: UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1))
                        otherTitle.textAlignment = .Left
                        otherTitle.text = "其他费用"
                        otherTitle.hidden = true
                        otherTitle.tag = 15004
                        cell.contentView.addSubview(otherTitle)
                        
                        let getPriceName = UILabel.create(CGRectMake(15, CGRectGetMaxY(otherTitle.frame)+10, 100, 20), font: UIFont.systemFontOfSize(14), textColor: UIColor.init(red: 74/255.0, green: 74/255.0, blue: 74/255.0, alpha: 1))
                        getPriceName.textAlignment = .Left
                        getPriceName.text = "取车费用"
                        getPriceName.hidden = true
                        getPriceName.tag = 15005
                        cell.contentView.addSubview(getPriceName)
                        
                        let getPriceLabel = UILabel.create(CGRectMake(0, CGRectGetMinY(getPriceName.frame), tvList.frame.size.width-70, 20), font: UIFont.systemFontOfSize(14), textColor: UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1))
                        getPriceLabel.textAlignment = .Right
                        getPriceLabel.tag = 15002
                        getPriceLabel.hidden = true
                        cell.contentView.addSubview(getPriceLabel)
                        
                        let backPriceName = UILabel.create(CGRectMake(15, CGRectGetMaxY(getPriceName.frame)+10, 100, 20), font: UIFont.systemFontOfSize(14), textColor: UIColor.init(red: 74/255.0, green: 74/255.0, blue: 74/255.0, alpha: 1))
                        backPriceName.textAlignment = .Left
                        backPriceName.text = "还车费用"
                        backPriceName.hidden = true
                        backPriceName.tag = 15006
                        cell.contentView.addSubview(backPriceName)
                        
                        let backPriceLabel = UILabel.create(CGRectMake(0, CGRectGetMinY(backPriceName.frame), tvList.frame.size.width-70, 20), font: UIFont.systemFontOfSize(14), textColor: UIColor.init(red: 155/255.0, green: 155/255.0, blue: 155/255.0, alpha: 1))
                        backPriceLabel.textAlignment = .Right
                        backPriceLabel.tag = 15003
                        backPriceLabel.hidden = true
                        cell.contentView.addSubview(backPriceLabel)
                    }
                } else {
                    if carstatus != nil && carstatus < 3{
                        var lbl = UILabel.create(CGRectMake(CGFloat(110), CGFloat(15), 100, 13), font: UIFont.systemFontOfSize(12), textColor:UIColor.grayColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                        //                    lbl.text = "用户备注信息"
                        lbl.tag = 430
                        cell.contentView.addSubview(lbl)
                        
                        lbl = UILabel.create(CGRectMake(CGFloat(110), CGFloat(35), 400, 15), font: UIFont.systemFontOfSize(15), textColor:ORANGECOLOR)//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                        lbl.tag = 431
                        cell.contentView.addSubview(lbl)
                        
                        let imgvSW = SWImageView.init(frame: CGRectMake(15, 15, 0, 0))
                        //                    imgv.image = UIImage(named: "picture.png")
                        imgvSW.tag = 432
                        cell.contentView.addSubview(imgvSW)
                        
                        lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(75), 75, 15), font: UIFont.systemFontOfSize(9), textColor:UIColor.whiteColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                        lbl.textAlignment = NSTextAlignment.Center
                        lbl.tag = 436
                        cell.contentView.addSubview(lbl)
                        
                        //                    var imgv = UIImageView.init(frame: CGRectMake(15, 104, tvList.frame.size.width-30, 1))
                        //                    imgv.image = UIImage(named: "dashe_ line.png")
                        //                    imgv.tag = 433
                        //                    cell.contentView.addSubview(imgv)
                        
                        var btn = UIButton(type: .Custom)
                        btn.frame = CGRectMake(15, 15, 0, 0)
                        btn.backgroundColor = UIColor.clearColor()
                        btn.tag = 434
                        btn.addTarget(self, action: #selector(SHOrdersListView.btnShowPictureClicked(_:)), forControlEvents: .TouchUpInside)
                        cell.contentView.addSubview(btn)
                        
                        
                        btn = UIButton(type: .Custom)
                        btn.frame = CGRectMake(110, 71, 95, 28)
                        //                    btn.setBackgroundImage(UIImage(named: "voice.png"), forState: UIControlState.Normal)
                        btn.setBackgroundImage(UIImage(named: "voice.png"), forState: .Normal)
                        btn.setBackgroundImage(UIImage(named: "voice_play.png"), forState: .Selected)
                        btn.titleLabel?.font = UIFont.boldSystemFontOfSize(14)
                        btn.setTitleColor(UIColor.grayColor(), forState: .Normal)
                        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, -40)
                        
                        btn.tag = 435
                        btn.hidden = true
                        btn.addTarget(self, action: #selector(SHOrdersListView.audioClicked), forControlEvents: .TouchUpInside)
                        cell.contentView.addSubview(btn)
                        
                        btnPlay = btn
                    } else {
                        let imgv = UIImageView.init(frame: CGRectMake(15, 0, tvList.frame.size.width-30, 1))
                        imgv.image = UIImage(named: "dashe_ line.png")
                        imgv.tag = 610
                        cell.contentView.addSubview(imgv)
                        
                        var lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(20), 100, 13), font: UIFont.systemFontOfSize(12), textColor: UIColor.grayColor())
                        lbl.tag = 600
                        lbl.textAlignment = .Left
                        cell.contentView.addSubview(lbl)
                        
                        lbl = UILabel.create(CGRectMake(CGFloat(200), CGFloat(20), 100, 13), font: UIFont.systemFontOfSize(12), textColor: UIColor.grayColor())
                        lbl.tag = 620
                        lbl.textAlignment = .Left
                        cell.contentView.addSubview(lbl)
                        
                        lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(43), 200, 13), font: UIFont.systemFontOfSize(12), textColor: UIColor.grayColor())
                        lbl.tag = 640
                        lbl.textAlignment = .Left
                        cell.contentView.addSubview(lbl)
                        
                        lbl = UILabel.create(CGRectMake(CGFloat(200), CGFloat(43), 200, 13), font: UIFont.systemFontOfSize(12), textColor: UIColor.grayColor())
                        lbl.tag = 680
                        lbl.textAlignment = .Left
                        cell.contentView.addSubview(lbl)
                    }
                }
                
                
                
                if indexPath.row == 0 {
                    let lblConnectPersonTitle = cell.contentView.viewWithTag(20000) as! UILabel!
                    let lblOrderTimeTitle = cell.contentView.viewWithTag(20001) as! UILabel!
                    let lblConnectPhoneTitle = cell.contentView.viewWithTag(20002) as! UILabel!
                    let lblPickerTypeTitle = cell.contentView.viewWithTag(20003) as! UILabel!
                    
                    
                    let lblConnectPerson = cell.contentView.viewWithTag(30000) as! UILabel!
                    let lblOrderTime = cell.contentView.viewWithTag(30001) as! UILabel!
                    let lblConnectPhone = cell.contentView.viewWithTag(30002) as! UILabel!
                    let lblPickerType = cell.contentView.viewWithTag(30003) as! UILabel!
                    //                lblOrderTime.frame = CGRectMake(465, 35, 200, 15)
                    
                    lblConnectPersonTitle.text = "预约人"
                    lblOrderTimeTitle.text = "预约时间"
                    lblConnectPhoneTitle.text = "联系电话"
                    lblPickerTypeTitle.text = "服务方式"
                    
                    lblConnectPerson.text = OrdersInfo?.objectForKey("name") as! String?
                    if let phone = OrdersInfo?.objectForKey("order_phone") as! String!{
                        lblConnectPhone.text = phone
                    }else if let phone = OrdersInfo?.objectForKey("phone") as! String!{
                        lblConnectPhone.text = phone
                    }else{
                        lblConnectPhone.text = nil
                    }
                    if let pickType = OrdersInfo?.objectForKey("pick_type")?.intValue{
                        if 1==pickType{
                            lblPickerType.text = "开车到店"
                        }else{
                            lblPickerType.text = "上门取车"
                        }
                    }else{
                        lblPickerType.text = nil
                    }
                    
                    lblOrderTime.text = OrdersInfo?.objectForKey("order_time") as! String?
                    lblConnectPhone.textColor = ORANGECOLOR
                } else if indexPath.row == 1 {
                    //"2318"
                    if let order_id = self.OrdersInfo?.objectForKey("order_id") as! String!{
                        let dict = SHDataProvider.getNewOrderDetail(["order_id":order_id,"is_access_token":"1","account_type":"1"])
                        let processDic = dict?.objectForKey("data")?.objectForKey("process")
                        var timeName = ["comedate","start_time","end_time","created_time","pay_time","outdate",""]
                       //\\
//                        let priceNumber = cell.contentView.viewWithTag(2100) as! UILabel!
                        
                        if processDic != nil {
                            workorder = processDic?.objectForKey("workorder") as! String
                            let imageView = cell.contentView.viewWithTag(1002) as! UIImageView!
                            let priceLabel = cell.contentView.viewWithTag(1004) as! UILabel!
                            
                            let number = processDic?.objectForKey("car_state")?.integerValue
                            var numberimage : NSString
                            if number == 0 {
                                numberimage = "0"
                            } else if number == 1 || number == 2 || number == 3 || number == 4 {
                                numberimage = "1"
                            } else if number == 5 {
                                numberimage = "2"
                            } else if number == 10 {
                                numberimage = "3"
                            } else if number == 11 {
                                numberimage = "4"
                            } else if number == 12 {
                                numberimage = "5"
                            } else {
                                numberimage = "0"
                            }
                            
                            let orderStatus = OrdersInfo?.objectForKey("order_status")?.integerValue
                            
                            if orderStatus == 4 || orderStatus == 5 {
                                numberimage = "6"
                            }
                            
                            carstatus = numberimage.integerValue
                            
//                            if carstatus > 2 {
//                                priceNumber.hidden = false
//                            } else {
//                                priceNumber.hidden = true
//                            }
                            
                            if carstatus > 3 {
                                successView.text = "订单已支付"
                                if orderStatus == 17 || orderStatus == 12{
                                    successView.text = ""
                                }
                            } else if carstatus == 3 {
                                successView.text = "订单待支付"
                            }
                            
                            if carstatus == 6 {
                                successView.text = "订单已完成"
                            }
                            
                            if carstatus < 3 {
                                vFooterBG.hidden = true
                            } else {
                                vFooterBG.hidden = false
                            }
                            
//                            if let pickType = OrdersInfo?.objectForKey("pick_type")?.intValue{
//                                if 1==pickType{
//                                    imageView.image = UIImage(named: "rate_of_progress".stringByAppendingString(numberimage as String))
//                                }else{
                                    imageView.image = UIImage(named: "rate_of_progress7".stringByAppendingString(numberimage as String))
//                                }
//                            } else {
//                                imageView.image = UIImage(named: "rate_of_progress".stringByAppendingString(numberimage as String))
//                            }
                            var moneyString = processDic?.objectForKey("money")?.floatValue
                            if moneyString != nil {
                                if let info = OrdersInfo?.objectForKey("getcar_info") {
                                    if let cprice = info.objectForKey("c_price") {
                                        moneyString = moneyString! + cprice.floatValue
                                    }
                                }
                                
                                if let info = OrdersInfo?.objectForKey("sendcar_info") {
                                    if let cprice = info.objectForKey("c_price") {
                                        moneyString = moneyString! + cprice.floatValue
                                    }
                                }
//                                priceLabel.text = "¥".stringByAppendingString(moneyString as! String) as! String
                                priceLabel.text = NSString(format: "¥%.2lf", moneyString!) as String
                            }
                            
                            var titles = ["派工：","开工：","完工：","结算：","收银：","出厂："]
                            let index = carstatus > 5 ? 5 : carstatus
                            for i in 0 ..< index+1 {
                                let lbl = cell.contentView.viewWithTag(1005+i) as! UILabel!
                                let timeString = processDic?.objectForKey(timeName[i]) as? String
                                if timeString != nil {
                                    lbl.text = titles[i].stringByAppendingString(timeString!)
                                } else {
                                    lbl.text = ""
                                }
                            }
                        }
                    }
                    
                } else if indexPath.row == 2 {
                    let lblReturnCarAddressTitle = cell.contentView.viewWithTag(410) as! UILabel!
                    let lblReturnCarAddress = cell.contentView.viewWithTag(411) as! UILabel!
                    
                    if let getCarInfo = OrdersInfo?.objectForKey("getcar_info") as! NSDictionary!{
                        lblReturnCarAddressTitle.text = "取车信息："
                        
                        if let meter = getCarInfo.objectForKey("c_mileage") as! String!{
                            let address = getCarInfo.objectForKey("c_address") as? String
                            lblReturnCarAddress.text = "\(address)(\(meter)KM)"
                        }else{
                            let address = getCarInfo.objectForKey("c_address") as? String
                            lblReturnCarAddress.text = "\(address))"
                        }
                    }
                    
                    if let sendCarInfo = OrdersInfo?.objectForKey("sendcar_info") as! NSDictionary!{
                        lblReturnCarAddressTitle.text = "送车信息："
                        if let meter = sendCarInfo.objectForKey("c_mileage") as! String!{
                            let address = sendCarInfo.objectForKey("c_address") as? String
                            lblReturnCarAddress.text = "\(address)(\(meter)KM)"
                        }else{
                            let address = sendCarInfo.objectForKey("c_address") as? String
                            lblReturnCarAddress.text = "\(address))"
                        }
                    }else{
                        if let getCarInfo = OrdersInfo?.objectForKey("getcar_info") as! NSDictionary!{
                            lblReturnCarAddressTitle.text = "取车信息："
                            if let meter = getCarInfo.objectForKey("c_mileage") as! String!{
                                let address = getCarInfo.objectForKey("c_address") as? String
                                lblReturnCarAddress.text = "\(address)(\(meter)KM)"
                            }else{
                                let address = getCarInfo.objectForKey("c_address") as? String
                                lblReturnCarAddress.text = "\(address))"
                            }
                        }else{
                            lblReturnCarAddressTitle.text = "店铺地址："
                            
                            if ((OrdersInfo?.objectForKey("dealer_info")) != nil) {
                                lblReturnCarAddress.text = OrdersInfo?.objectForKey("dealer_info")!.objectForKey("address") as? String
                            }
                            
                        }
                    }
                    
                } else if indexPath.row == 3 {
                    
                    if carstatus != nil && carstatus < 3{
                        let lblServiceContentTitle = cell.contentView.viewWithTag(420) as! UILabel!
                        let lblServiceContent = cell.contentView.viewWithTag(421) as! UILabel!
                        let imgvLineThree = cell.contentView.viewWithTag(422) as! UIImageView!
                        lblServiceContentTitle.text = "服务内容"
                        
                        
//                        order_type：1是保养   2是维修  3是维修并保养
                        
                        if let content = OrdersInfo?.objectForKey("content") as! String!{
                            lblServiceContent.text = content
                        }else{
                            lblServiceContent.text = nil
                        }
                        
                        //                lblTitle.text = "保养内容："
                        var strRepair : NSString
                        if let repairContent = OrdersInfo?.objectForKey("content") as! String!{
                            strRepair = repairContent
                        }else{
                            strRepair = ""
                        }
                        
                        var strMaintain :NSString
                        if let upkeepType = OrdersInfo?.objectForKey("upkeep_type") as! String!{
                            var ary = upkeepType.componentsSeparatedByString(",")
                            var aryName = ["机油","机油过滤器","空气滤清器","空调空气滤清器","冷却液","油滤器","燃油滤清器","火花塞","制动液","手动变速箱油","自动变速箱油","发动机正时皮带"]
                            let str = NSMutableString()
                            for i in 0 ..< ary.count{
                                let idx = ary[i]
                                
                                //                        var intVal:Int = idx.toint()
                                if idx != ""{
                                    str.appendString(aryName[Int(idx)!])
                                    str.appendString("、")
                                }
                            }
                            //                    var txt = str.replaceCharactersInRange(NSMakeRange(str.length - 1, 1), withString: "") as? String
                            strMaintain = str.substringWithRange(NSMakeRange(0, str.length-1))
                            
                        }else{
                            
                            if orderType.isEqualToString("1") {
                                strMaintain = "保养"
                            } else if orderType.isEqualToString("2") {
                                strMaintain = "维修"
                            } else if orderType.isEqualToString("3") {
                                strMaintain = "维修并保养"
                            } else {
                                strMaintain = ""
                            }
                        }
                        
                        if strRepair.length > 0 && strMaintain.length>0 {
                            lblServiceContent.text = "维修：\(strRepair)\n保养：\(strMaintain)"
                        }else if strRepair.length > 0 && strMaintain.length==0{
                            lblServiceContent.text = strRepair as String
                        }else  if strRepair.length == 0 && strMaintain.length>0{
                            lblServiceContent.text =  strMaintain as String
                        }
                        //            else{
                        //                lblServiceContent.text = strMaintain as String
                        //            }
                        
                        lblServiceContent.numberOfLines = 0
                        /// 计算文本大小
                        if lblServiceContent!.text != nil {
                            let attributes = [NSFontAttributeName: lblServiceContent!.font]
                            let option = NSStringDrawingOptions.UsesLineFragmentOrigin
                            let text: NSString = NSString(CString: lblServiceContent!.text!.cStringUsingEncoding(NSUTF8StringEncoding)!,
                                                          encoding: NSUTF8StringEncoding)!
                            var rect = text.boundingRectWithSize(CGSizeMake(tvList.frame.size.width-30, 1000), options: option, attributes: attributes, context: nil)
                            rect.origin.x = 15
                            rect.origin.y = 35
                            cellRowThreeHeight = rect.size.height
                            if rect.width>580{
                                lblServiceContent.numberOfLines = 10;
                                lblServiceContent.frame = CGRectMake(15, 30, tvList.frame.size.width-30, cellRowThreeHeight)
                            }else{
                                lblServiceContent.numberOfLines = 10;
                                lblServiceContent.frame = CGRectMake(15, 35, tvList.frame.size.width-30, cellRowThreeHeight)
                            }
                            //                    var width = rect.size.width + 24
                            //                     var height = rect.size.height + 80
                            
                            
                        }
                        
                        let imgvY:CGFloat =  cellRowThreeHeight == 0 ? 64:cellRowThreeHeight+50>65 ?cellRowThreeHeight+50-1:64
                        imgvLineThree.frame = CGRectMake(15, imgvY, tvList.frame.size.width-30, 1)
                    } else {
                        //\\
//                        var rep_items : NSDictionary = ["work_time_price":"550.70"]
                        
                        let workPriceLabel = cell.contentView.viewWithTag(15001) as! UILabel!
                        let getPriceLabel = cell.contentView.viewWithTag(15002) as! UILabel!
                        let backPriceLabel = cell.contentView.viewWithTag(15003) as! UILabel!
                        let otherTitle = cell.contentView.viewWithTag(15004) as! UILabel!
                        let getPriceName = cell.contentView.viewWithTag(15005) as! UILabel!
                        let backPriceName = cell.contentView.viewWithTag(15006) as! UILabel!
                        let workPriceTitle = cell.contentView.viewWithTag(15007) as! UILabel!
                        let workPriceNumber = cell.contentView.viewWithTag(15008) as! UILabel!
                        
                        //"WXDT25A161219001"
                        if workorder != nil {
                            let dictt = SHDataProvider.getWorkOrderInfo(["order_id":workorder,"is_access_token":"1","account_type":"1"])
                            if dictt != nil {
                                let dict = dictt?.objectForKey("data")?.objectForKey("info")?.objectForKey("ly_part") as! NSArray
                                let repDict = dictt?.objectForKey("data")?.objectForKey("info")?.objectForKey("rep_items") as! NSArray
                                let dictArray:NSMutableArray = []
                                for i in 0 ..< dict.count {
                                    let type = dict[i].objectForKey("cn_name")
                                    if type?.isEqualToString("") == false {
                                        dictArray.addObject(dict[i])
                                    }
                                }
                                
                                var repDictnumber : Float = 0
                                for i in 0 ..< repDict.count {
                                    let number = repDict[i].objectForKey("after_money")?.floatValue
                                    repDictnumber += number!
                                }
                                
                                workPriceLabel.text = NSString.init(format: "%.2lf", repDictnumber) as String
                                
                                let number = dictArray.count > 30 ? 30 : dictArray.count
                                
                                for i in 0 ..< number {
                                    let nameLabel = cell.contentView.viewWithTag(1100+i) as! UILabel!
                                    let numberLabel = cell.contentView.viewWithTag(1200+i) as! UILabel!
                                    let originalPriceLabel = cell.contentView.viewWithTag(1300+i) as! UILabel!
                                    let priceLabel = cell.contentView.viewWithTag(1400+i) as! UILabel!
                                    let lineLabel = cell.contentView.viewWithTag(1600+i) as! UILabel!
                                    
                                    nameLabel.hidden = false
                                    numberLabel.hidden = false
                                    originalPriceLabel.hidden = false
                                    priceLabel.hidden = false
                                    lineLabel.hidden = false
                                    
                                    nameLabel.text = dictArray[i].objectForKey("cn_name") as? String
                                    numberLabel.text = NSString.init(format: "x%@", dictArray[i].objectForKey("f6") as! String) as String
                                    originalPriceLabel.text = NSString.init(format: "¥%@", dictArray[i].objectForKey("before_money") as! String) as String
                                    originalPriceLabel.sizeToFit()
                                    originalPriceLabel.frame = CGRectMake(tvList.frame.size.width-200-CGRectGetWidth(originalPriceLabel.frame), CGRectGetMinY(numberLabel.frame), CGRectGetWidth(originalPriceLabel.frame), 20)
                                    lineLabel.frame = CGRectMake(CGRectGetMinX(originalPriceLabel.frame)-2, CGRectGetMidY(originalPriceLabel.frame), CGRectGetWidth(originalPriceLabel.frame)+4, 1)
                                    priceLabel.text = NSString.init(format: "¥%@", dictArray[i].objectForKey("f11") as! String) as String
                                }
                                
                                workPriceTitle.frame = CGRectMake(15, 37+CGFloat(10+30*number), 100, 17)
                                workPriceNumber.frame = CGRectMake(15, CGRectGetMaxY(workPriceTitle.frame)+10, 100, 20)
                                workPriceLabel.frame = CGRectMake(0, CGRectGetMinY(workPriceNumber.frame), tvList.frame.size.width-70, 20)
                                otherTitle.frame = CGRectMake(15, CGRectGetMaxY(workPriceNumber.frame)+10, 100, 20)
                                getPriceName.frame = CGRectMake(15, CGRectGetMaxY(otherTitle.frame)+10, 100, 20)
                                getPriceLabel.frame = CGRectMake(0, CGRectGetMinY(getPriceName.frame), tvList.frame.size.width-70, 20)
                                backPriceName.frame = CGRectMake(15, CGRectGetMaxY(getPriceName.frame)+10, 100, 20)
                                backPriceLabel.frame = CGRectMake(0, CGRectGetMinY(backPriceName.frame), tvList.frame.size.width-70, 20)
                            }
                        }
                        
                        
                        
                        
                        let type = OrdersInfo?.objectForKey("pick_type")
                        
                        if type?.isEqualToString("2") == true {
                            otherTitle.hidden = false
                            getPriceName.hidden = false
                            backPriceName.hidden = false
                            getPriceLabel.hidden = false
                            backPriceLabel.hidden = false
                            
                            if let info = OrdersInfo?.objectForKey("getcar_info") {
                                if info.objectForKey("c_price") != nil {
                                    let getString = OrdersInfo?.objectForKey("getcar_info")?.objectForKey("c_price") as! String
                                    getPriceLabel.text = NSString.init(format: "¥%@", getString) as String
                                }
                            }
                            
                            if let info = OrdersInfo?.objectForKey("sendcar_info") {
                                if info.objectForKey("c_price") != nil {
                                    let sendString = OrdersInfo?.objectForKey("sendcar_info")?.objectForKey("c_price") as! String
                                    backPriceLabel.text = NSString.init(format: "¥%@", sendString) as String
                                }
                            }
                            
                        } else {
                            otherTitle.hidden = true
                            getPriceName.hidden = true
                            backPriceName.hidden = true
                            getPriceLabel.hidden = true
                            backPriceLabel.hidden = true
                        }
                    }
                } else {
                    if carstatus != nil && carstatus < 3{
                        let lblRemarkTitle = cell.contentView.viewWithTag(430) as! UILabel!
                        let lblRemarkContent = cell.contentView.viewWithTag(431) as! UILabel!
                        let lblPictureCount = cell.contentView.viewWithTag(436) as! UILabel!
                        let imgvPicture  = cell.contentView.viewWithTag(432) as! SWImageView
                        let  btnShowImage = cell.contentView.viewWithTag(434) as! UIButton!
                        let  btnVoice = cell.contentView.viewWithTag(435) as! UIButton!
                        
                        let photoList = OrdersInfo?.objectForKey("photo_list") as! NSArray!
                        if photoList?.count > 0 {
                            imgvPicture.frame = CGRectMake(15, 15, 75, 75)
                            imgvPicture.loadURL(photoList[0].objectForKey("smallUrl") as? String)
                            //                    imgvPicture.image = UIImage(named: photoList.objectAtIndex(0) as! String)
                            btnShowImage.hidden = false
                            btnShowImage.frame = CGRectMake(15, 15, 75, 75)
                            lblRemarkTitle.frame = CGRectMake(110, 15, 100, 13)
                            lblRemarkContent.frame = CGRectMake(110, 35, 500, 13)
                            btnVoice.frame = CGRectMake(110, 71, 95, 28)
                            lblPictureCount.text = "查看全部\(photoList.count)张照片"
                            
                        }
                        else{
                            btnShowImage.hidden = true
                            imgvPicture.frame = CGRectMake(15, 15, 0, 0)
                            imgvPicture.image = nil
                            lblPictureCount.text = ""
                            lblRemarkTitle.frame = CGRectMake(15, 15, 100, 13)
                            lblRemarkContent.frame = CGRectMake(15, 35, 594, 13)
                            btnVoice.frame = CGRectMake(15, 71, 95, 28)
                            
                        }
                        
                        if player != nil{
                            btnVoice.hidden = false
                            if (OrdersInfo?.objectForKey("audio") as! String!) != nil{
                                //                    var path = audio.lastPathComponent.temporaryPath()
                                
                                //                    btnVoice.setTitle(String(format: "%.0lf\"", player!.duration), forState: .Normal)
                            }
                            
                            
                        }else{
                            if btnVoice != nil{
                                btnVoice.hidden = true
                            }
                        }
                        var strGetCarRemark : NSString
                        if let userGetRemark = OrdersInfo?.objectForKey("getcar_info")?.objectForKey("remark") as! String!{
                            //                lblRemarkContent.text = userRemark
                            strGetCarRemark = userGetRemark
                        }else{
                            //                lblRemarkContent.text = "未填写备注"
                            strGetCarRemark = ""
                        }
                        
                        var strSendCarRemark : NSString
                        if let userSendRemark = OrdersInfo?.objectForKey("sendcar_info")?.objectForKey("remark") as! String!{
                            strSendCarRemark = userSendRemark
                        }else{
                            strSendCarRemark = ""
                        }
                        
                        if strGetCarRemark.length > 0 && strSendCarRemark.length > 0 {
                            //                lblRemarkContent.text = "取车备注：\(strGetCarRemark)，送车备注：\(strSendCarRemark)"
                            lblRemarkTitle.text = "送车备注"
                            lblRemarkContent.text = "\(strSendCarRemark)"
                        }else if strGetCarRemark.length > 0 && strSendCarRemark.length == 0 {
                            lblRemarkTitle.text = "取车备注"
                            lblRemarkContent.text = "\(strGetCarRemark)"
                        }else if strGetCarRemark.length == 0 && strSendCarRemark.length > 0 {
                            lblRemarkTitle.text = "送车备注"
                            lblRemarkContent.text = "\(strSendCarRemark)"
                        }else{
                            lblRemarkTitle.text = "用户备注"
                            lblRemarkContent.text = "未填写备注"
                        }
                        
                        /// 计算文本大小
                        if lblRemarkContent!.text != nil {
                            let attributes = [NSFontAttributeName: lblRemarkContent!.font]
                            let option = NSStringDrawingOptions.UsesLineFragmentOrigin
                            let text: NSString = NSString(CString: lblRemarkContent!.text!.cStringUsingEncoding(NSUTF8StringEncoding)!,
                                                          encoding: NSUTF8StringEncoding)!
                            var rect = text.boundingRectWithSize(CGSizeMake(lblRemarkContent.frame.size.width, 40), options: option, attributes: attributes, context: nil)
                            rect.origin.x = lblRemarkContent.frame.origin.x
                            rect.origin.y = 35
                            
                            if rect.width>lblRemarkContent.frame.size.width-20{
                                lblRemarkContent.numberOfLines = 2;
                                lblRemarkContent.frame = CGRectMake(lblRemarkContent.frame.origin.x, 30, lblRemarkContent.frame.size.width, 40)
                            }else{
                                lblRemarkContent.numberOfLines = 1;
                                lblRemarkContent.frame = CGRectMake(lblRemarkContent.frame.origin.x, 35, lblRemarkContent.frame.size.width, 15)
                            }
                            //                    var width = rect.size.width + 24
                            //                     var height = rect.size.height + 80
                            
                            
                        }
                    } else {
                            let lblPersonnameTitle = cell.contentView.viewWithTag(600) as! UILabel
                            let imageLine = cell.contentView.viewWithTag(610) as! UIImageView
                            let lblPersonPhotoTitle = cell.contentView.viewWithTag(620) as! UILabel
                            let lblPersonname = cell.contentView.viewWithTag(640) as! UILabel
                            let lblPersonPhone = cell.contentView.viewWithTag(680) as! UILabel
                        
                        let status = OrdersInfo!.objectForKey("order_status") as! String!
                        if status == "6"{
                            lblPersonnameTitle.hidden = false
                            lblPersonPhotoTitle.hidden = false
                            lblPersonname.hidden = false
                            lblPersonPhone.hidden = false
                            
                            lblPersonnameTitle.text = "取车员"
                            lblPersonPhotoTitle.text = "联系电话"
                            
                            if let c_name = OrdersInfo?.objectForKey("getcar_info")?.objectForKey("name") as? String!{
                                lblPersonname.text = c_name
                            }else{
                                lblPersonname.text = nil
                            }
                            
                            if let c_phone = OrdersInfo?.objectForKey("getcar_info")?.objectForKey("telephone") as? String!{
                                lblPersonPhone.text = c_phone
                            }else{
                                lblPersonPhone.text = nil
                            }
                            
                        }else if status == "11" || status == "17"{
                            
                            lblPersonnameTitle.hidden = false
                            lblPersonPhotoTitle.hidden = false
                            lblPersonname.hidden = false
                            lblPersonPhone.hidden = false
                            imageLine.hidden = false
                            
                            lblPersonnameTitle.text = "送车员"
                            lblPersonPhotoTitle.text = "联系电话"
                            
                            if let c_name = OrdersInfo?.objectForKey("sendcar_info")?.objectForKey("name") as? String!{
                                lblPersonname.text = c_name
                            }else{
                                lblPersonname.text = nil
                            }
                            
                            if let c_phone = OrdersInfo?.objectForKey("sendcar_info")?.objectForKey("telephone") as? String!{
                                lblPersonPhone.text = c_phone
                                lblPersonPhone.textColor = ORANGECOLOR
                            }else{
                                lblPersonPhone.text = nil
                            }
                        }else{
                            lblPersonnameTitle.hidden = true
                            lblPersonPhotoTitle.hidden = true
                            lblPersonname.hidden = true
                            lblPersonPhone.hidden = true
                            imageLine.hidden = true
                        }
                    }
                }
            
                cell.selectionStyle = UITableViewCellSelectionStyle.None
                return cell
            }
        
        
        }
    
    
    
                if indexPath.row == 0 {
                    let titles = ["预约人","预约时间","联系电话","服务方式"]
                    for i in 0 ..< titles.count{
                        let col = i % 4
//                        let row = i / 2

                        var margin:Int = 0
    
                        if i > 1 {
                            margin = 100
                        }
    
                        var lbl = UILabel.create(CGRectMake(CGFloat(15+140*col+margin), CGFloat(15), 100, 13), font: UIFont.systemFontOfSize(12), textColor:UIColor.grayColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
//                        lbl.text = titles[i]
                        lbl.tag = 20000+i
                        cell.contentView.addSubview(lbl)
                        
                        lbl = UILabel.create(CGRectMake(CGFloat(15+140*col+margin), CGFloat(35), 100, 18), font: UIFont.systemFontOfSize(14), textColor: UIColor.blackColor())
                        //            lbl.text = titles[i]
                        lbl.tag = 30000+i
                        if i == 1{
                            lbl.frame = CGRectMake(CGFloat(15+140*col), 35, 200, 15)
                        }
                        cell.contentView.addSubview(lbl)
                    }
                    

                }else if indexPath.row == 1{
                    var lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(15), 100, 13), font: UIFont.systemFontOfSize(12), textColor:UIColor.grayColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
//                    lbl.text = "还车地址"
                    lbl.tag = 410
                    cell.contentView.addSubview(lbl)
                    
                    var imgv = UIImageView.init(frame: CGRectMake(15, 35, tvList.frame.size.width-30, 38))
                    imgv.image = UIImage(named: "retrun_car_addres_map.png")
                    cell.contentView.addSubview(imgv)
                    
                    imgv = UIImageView.init(frame: CGRectMake(25, 40, 20, 27))
                    imgv.image = UIImage(named: "location.png")
                    cell.contentView.addSubview(imgv)
                    
                    lbl = UILabel.create(CGRectMake(CGFloat(50), CGFloat(40), 560, 28), font: UIFont.systemFontOfSize(14), textColor:UIColor(red: 74/255.0, green: 74/255.0, blue: 74/255.0, alpha: 1))//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                    lbl.tag = 411
                    
                    cell.contentView.addSubview(lbl)
                    lbl = UILabel.create(CGRectMake(CGFloat(tvList.frame.size.width-15-350), CGFloat(15), 350, 15), font: UIFont.systemFontOfSize(12), textColor:UIColor(white: 0.25, alpha: 1))//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                    lbl.textAlignment = NSTextAlignment.Right
                    lbl.tag = 412
                    cell.contentView.addSubview(lbl)
                    
                    lbl = UILabel.create(CGRectMake(CGFloat(60), CGFloat(40), 500, 15), font: UIFont.systemFontOfSize(14), textColor:UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1))//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                    lbl.tag = 413
                    cell.contentView.addSubview(lbl)
                    
//                    var titles = ["预约人","联系电话","取车费用","取车时间"]
                    for i in 0 ..< 4{
                        let col = i % 4
                        var lbl = UILabel.create(CGRectMake(CGFloat(15+140*col), CGFloat(85), 100, 13), font: UIFont.systemFontOfSize(12), textColor:UIColor.grayColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                        //                        lbl.text = titles[i]
                        lbl.tag = 5000+i
                        cell.contentView.addSubview(lbl)
                        
                        lbl = UILabel.create(CGRectMake(CGFloat(15+140*col), CGFloat(105), 100, 18), font: UIFont.systemFontOfSize(14), textColor: UIColor.blackColor())
                        //            lbl.text = titles[i]
                        lbl.tag = 5100+i
                        if i == 3{
                            lbl.frame = CGRectMake(CGFloat(15+140*col), 105, 200, 15)
                        }
                        cell.contentView.addSubview(lbl)
                    }
                    
                }else if indexPath.row == 2{
                    var lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(15), 100, 13), font: UIFont.systemFontOfSize(12), textColor:UIColor.grayColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
//                    lbl.text = "服务内容"
                    lbl.tag = 420
                    cell.contentView.addSubview(lbl)
                    
                    lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(35), tvList.frame.size.width-30, 15), font: UIFont.systemFontOfSize(14), textColor:UIColor.blackColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                    lbl.tag = 421
                    cell.contentView.addSubview(lbl)
                    
                    let imgv = UIImageView.init(frame: CGRectMake(15, 74, tvList.frame.size.width-30, 1))
                    imgv.image = UIImage(named: "dashe_ line.png")
                    imgv.tag = 422
                    cell.contentView.addSubview(imgv)

                }else if indexPath.row == 3{
                    var lbl = UILabel.create(CGRectMake(CGFloat(110), CGFloat(15), 100, 13), font: UIFont.systemFontOfSize(12), textColor:UIColor.grayColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
//                    lbl.text = "用户备注信息"
                    lbl.tag = 430
                    cell.contentView.addSubview(lbl)
                    
                    lbl = UILabel.create(CGRectMake(CGFloat(110), CGFloat(35), 400, 15), font: UIFont.systemFontOfSize(15), textColor:ORANGECOLOR)//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                    lbl.tag = 431
                    cell.contentView.addSubview(lbl)
                    
                    let imgvSW = SWImageView.init(frame: CGRectMake(15, 15, 0, 0))
//                    imgv.image = UIImage(named: "picture.png")
                    imgvSW.tag = 432
                    cell.contentView.addSubview(imgvSW)
                    
                    lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(75), 75, 15), font: UIFont.systemFontOfSize(9), textColor:UIColor.whiteColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
                    lbl.textAlignment = NSTextAlignment.Center
                    lbl.tag = 436
                    cell.contentView.addSubview(lbl)
                    
//                    var imgv = UIImageView.init(frame: CGRectMake(15, 104, tvList.frame.size.width-30, 1))
//                    imgv.image = UIImage(named: "dashe_ line.png")
//                    imgv.tag = 433
//                    cell.contentView.addSubview(imgv)
                    
                    var btn = UIButton(type: .Custom)
                    btn.frame = CGRectMake(15, 15, 0, 0)
                    btn.backgroundColor = UIColor.clearColor()
                    btn.tag = 434
                    btn.addTarget(self, action: #selector(SHOrdersListView.btnShowPictureClicked(_:)), forControlEvents: .TouchUpInside)
                    cell.contentView.addSubview(btn)
                    
                    
                    btn = UIButton(type: .Custom)
                    btn.frame = CGRectMake(110, 71, 95, 28)
//                    btn.setBackgroundImage(UIImage(named: "voice.png"), forState: UIControlState.Normal)
                    btn.setBackgroundImage(UIImage(named: "voice.png"), forState: .Normal)
                    btn.setBackgroundImage(UIImage(named: "voice_play.png"), forState: .Selected)
                    btn.titleLabel?.font = UIFont.boldSystemFontOfSize(14)
                    btn.setTitleColor(UIColor.grayColor(), forState: .Normal)
                    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, -40)

                    btn.tag = 435
                    btn.hidden = true
                    btn.addTarget(self, action: #selector(SHOrdersListView.audioClicked), forControlEvents: .TouchUpInside)
                    cell.contentView.addSubview(btn)
                    
                    btnPlay = btn
                }else if indexPath.row == 4{
//                    var lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(15), 100, 20), font: UIFont.systemFontOfSize(20), textColor:UIColor.grayColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
////                    lbl.text = "订单状态"
//                    lbl.tag = 440
//                    cell.contentView.addSubview(lbl)
                    
//                        for i in 0 ..< 16{
//                            let col = i % 4
//                            let row = i / 1

//                            var lbl = UILabel.create(CGRectMake(CGFloat(65), CGFloat(50+55*row), 280, 13), font: UIFont.systemFontOfSize(12), textColor:UIColor.blackColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
//                            lbl.tag = 450+i
//                            cell.contentView.addSubview(lbl)
//                            
//                            lbl = UILabel.create(CGRectMake(CGFloat(65), CGFloat(70+55*row), 280, 13), font: UIFont.systemFontOfSize(12), textColor: UIColor.grayColor())
//                            lbl.tag = 470+i
//                            cell.contentView.addSubview(lbl)
//                            
//                            var imgv = UIImageView.init(frame: CGRectMake(CGFloat(15+17), CGFloat(40+55*row), 1, 55))
//                            imgv.tag = 500+i
//                            cell.contentView.addSubview(imgv)
//                            
//                            imgv = UIImageView.init(frame: CGRectMake(CGFloat(15), CGFloat(50+55*row), 35, 35))
//                            imgv.tag = 520+i
//                            cell.contentView.addSubview(imgv)
//                            
//                            lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(50+55*row), 35, 35), font: UIFont.systemFontOfSize(14), textColor: UIColor.grayColor())
//                            lbl.textAlignment = NSTextAlignment.Center
//                            lbl.tag = 540+i
//                            cell.contentView.addSubview(lbl)
//                            
//                            
//                            imgv = UIImageView.init(frame: CGRectMake(CGFloat(345), CGFloat(40+55*row), 240, 50))
//                            imgv.tag = 560+i
//                            cell.contentView.addSubview(imgv)
//                            
//                            let imgvTX = SWImageView.init(frame: CGRectMake(CGFloat(350), CGFloat(45+55*row), 40, 40))
//                            imgvTX.tag = 580+i
//                            cell.contentView.addSubview(imgvTX)
                    
                            let imgv = UIImageView.init(frame: CGRectMake(15, 0, tvList.frame.size.width-30, 1))
                            imgv.image = UIImage(named: "dashe_ line.png")
                            imgv.tag = 610
                            cell.contentView.addSubview(imgv)
                    
                            var lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(20), 100, 13), font: UIFont.systemFontOfSize(12), textColor: UIColor.grayColor())
                            lbl.tag = 600
                            lbl.textAlignment = .Left
                            cell.contentView.addSubview(lbl)
                            
                            lbl = UILabel.create(CGRectMake(CGFloat(200), CGFloat(20), 100, 13), font: UIFont.systemFontOfSize(12), textColor: UIColor.grayColor())
                            lbl.tag = 620
                            lbl.textAlignment = .Left
                            cell.contentView.addSubview(lbl)
                            
                            lbl = UILabel.create(CGRectMake(CGFloat(15), CGFloat(43), 200, 13), font: UIFont.systemFontOfSize(12), textColor: UIColor.grayColor())
                            lbl.tag = 640
                            lbl.textAlignment = .Left
                            cell.contentView.addSubview(lbl)
                            
                            lbl = UILabel.create(CGRectMake(CGFloat(200), CGFloat(43), 200, 13), font: UIFont.systemFontOfSize(12), textColor: UIColor.grayColor())
                            lbl.tag = 680
                            lbl.textAlignment = .Left
                            cell.contentView.addSubview(lbl)
//                        }

                }
    
//            }
        
//            let lblTitle = cell.contentView.viewWithTag(100) as! UILabel!
//            let lblDetail = cell.contentView.viewWithTag(101) as! UILabel!
        
        
        if indexPath.row == 0{
            //标题名字
            let lblConnectPersonTitle = cell.contentView.viewWithTag(20000) as! UILabel!
            let lblOrderTimeTitle = cell.contentView.viewWithTag(20001) as! UILabel!
            let lblConnectPhoneTitle = cell.contentView.viewWithTag(20002) as! UILabel!
            let lblPickerTypeTitle = cell.contentView.viewWithTag(20003) as! UILabel!
            
 
            let lblConnectPerson = cell.contentView.viewWithTag(30000) as! UILabel!
            let lblOrderTime = cell.contentView.viewWithTag(30001) as! UILabel!
            let lblConnectPhone = cell.contentView.viewWithTag(30002) as! UILabel!
            let lblPickerType = cell.contentView.viewWithTag(30003) as! UILabel!
            //                lblOrderTime.frame = CGRectMake(465, 35, 200, 15)
            
            lblConnectPersonTitle.text = "预约人"
            lblOrderTimeTitle.text = "预约时间"
            lblConnectPhoneTitle.text = "联系电话"
            lblPickerTypeTitle.text = "服务方式"
            
            lblConnectPerson.text = OrdersInfo?.objectForKey("name") as! String?
            if let phone = OrdersInfo?.objectForKey("order_phone") as! String!{
                lblConnectPhone.text = phone
            }else if let phone = OrdersInfo?.objectForKey("phone") as! String!{
                lblConnectPhone.text = phone
            }else{
                lblConnectPhone.text = nil
            }
            if let pickType = OrdersInfo?.objectForKey("pick_type")?.intValue{
                if 1==pickType{
                    lblPickerType.text = "开车到店"
                }else{
                    lblPickerType.text = "上门取车"
                }
            }else{
                lblPickerType.text = nil
            }
            
            lblOrderTime.text = OrdersInfo?.objectForKey("order_time") as! String?
            lblConnectPhone.textColor = ORANGECOLOR
        }else  if indexPath.row == 1{
            let lblReturnCarAddressTitle = cell.contentView.viewWithTag(410) as! UILabel!
            let lblReturnCarAddress = cell.contentView.viewWithTag(411) as! UILabel!
            let lblVIN = cell.contentView.viewWithTag(412) as! UILabel!
//\\never used            let lblReturnCarTime = cell.contentView.viewWithTag(413) as! UILabel!
            
            
            //标题名字
            let lblCarConnectPersonTitle = cell.contentView.viewWithTag(5000) as! UILabel!
            let lblCarConnectPhoneTitle = cell.contentView.viewWithTag(5001) as! UILabel!
            let lblCarPickerTypeTitle = cell.contentView.viewWithTag(5002) as! UILabel!
            let lblCarOrderTimeTitle = cell.contentView.viewWithTag(5003) as! UILabel!
            
            
            let lblCarConnectPerson = cell.contentView.viewWithTag(5100) as! UILabel!
            let lblCarConnectPhone = cell.contentView.viewWithTag(5101) as! UILabel!
            let lblCarPickerType = cell.contentView.viewWithTag(5102) as! UILabel!
            let lblCarOrderTime = cell.contentView.viewWithTag(5103) as! UILabel!
            //                lblOrderTime.frame = CGRectMake(465, 35, 200, 15)
            
            if let getCarInfo = OrdersInfo?.objectForKey("getcar_info") as! NSDictionary!{
                lblCarConnectPersonTitle.text = "联系人"
                lblCarConnectPhoneTitle.text = "联系电话"
                lblCarPickerTypeTitle.text = "取车费用"
                lblCarOrderTimeTitle.text = "取车时间"
                
                lblCarConnectPerson.text = getCarInfo.objectForKey("c_name") as? String == nil ? "" : getCarInfo.objectForKey("c_name") as? String
                lblCarConnectPhone.text = getCarInfo.objectForKey("c_phone") as? String == nil ? "" : getCarInfo.objectForKey("c_phone") as? String
//                lblCarPickerType.text = "￥\(getCarInfo.objectForKey("c_price") as? String)"
                if let price = getCarInfo.objectForKey("c_price")?.floatValue{
                
                    lblCarPickerType.text = "￥\(price)"
                }
                lblCarOrderTime.text = getCarInfo.objectForKey("start_time") as? String == nil ? "" : getCarInfo.objectForKey("start_time") as? String
                lblReturnCarAddressTitle.text = "取车信息："
//                lblReturnCarAddress.text = getCarInfo.objectForKey("c_address") as? String
                if let meter = getCarInfo.objectForKey("c_mileage") as! String!{
                    let address = getCarInfo.objectForKey("c_address") as? String == nil ? "" : getCarInfo.objectForKey("c_address") as! String
                    lblReturnCarAddress.text = "\(address)(\(meter)KM)"
                }else{
                    let address = getCarInfo.objectForKey("c_address") as? String == nil ? "" : getCarInfo.objectForKey("c_address") as! String
                    lblReturnCarAddress.text = "\(address))"
                }
            }
            
            if let sendCarInfo = OrdersInfo?.objectForKey("sendcar_info") as! NSDictionary!{
                lblCarConnectPersonTitle.text = "联系人"
                lblCarConnectPhoneTitle.text = "联系电话"
                lblCarPickerTypeTitle.text = "送车费用"
                lblCarOrderTimeTitle.text = "送车时间"
                lblCarConnectPerson.text = sendCarInfo.objectForKey("c_name") as? String
                lblCarConnectPhone.text = sendCarInfo.objectForKey("c_phone") as? String
//                lblCarPickerType.text = "￥\(sendCarInfo.objectForKey("c_price") as? String)"
                if let price = sendCarInfo.objectForKey("c_price")?.floatValue{
                    lblCarPickerType.text = "￥\(price)"
                }
                lblCarOrderTime.text = sendCarInfo.objectForKey("start_time") as? String
                lblReturnCarAddressTitle.text = "送车信息："
//                lblReturnCarAddress.text = sendCarInfo.objectForKey("c_address") as? String
                if let meter = sendCarInfo.objectForKey("c_mileage") as! String!{
                    let address = sendCarInfo.objectForKey("c_address") as? String
                    lblReturnCarAddress.text = "\(address)(\(meter)KM)"
                }else{
                    let address = sendCarInfo.objectForKey("c_address") as? String
                    lblReturnCarAddress.text = "\(address))"
                }
            }else{
                if let getCarInfo = OrdersInfo?.objectForKey("getcar_info") as! NSDictionary!{
                    lblCarConnectPersonTitle.text = "联系人"
                    lblCarConnectPhoneTitle.text = "联系电话"
                    lblCarPickerTypeTitle.text = "取车费用"
                    lblCarOrderTimeTitle.text = "取车时间"
                    
                    lblCarConnectPerson.text = getCarInfo.objectForKey("c_name") as? String
                    lblCarConnectPhone.text = getCarInfo.objectForKey("c_phone") as? String
//                    lblCarPickerType.text = "￥\(getCarInfo.objectForKey("c_price") as? String)"
                    if let price = getCarInfo.objectForKey("c_price")?.floatValue{
                        lblCarPickerType.text = "￥\(price)"
                    }
                    lblCarOrderTime.text = getCarInfo.objectForKey("start_time") as? String
                    lblReturnCarAddressTitle.text = "取车信息："
//                    lblReturnCarAddress.text = getCarInfo.objectForKey("c_address") as? String
                    if let meter = getCarInfo.objectForKey("c_mileage") as! String!{
                        let address = getCarInfo.objectForKey("c_address") as? String
                        lblReturnCarAddress.text = "\(address)(\(meter)KM)"
                    }else{
                        let address = getCarInfo.objectForKey("c_address") as? String
                        lblReturnCarAddress.text = "\(address))"
                    }
                }else{
                    lblCarConnectPersonTitle.text = "联系人"
                    lblCarConnectPhoneTitle.text = "联系电话"
                    lblCarPickerTypeTitle.text = "取车费用"
                    lblCarOrderTimeTitle.text = "取车时间"
                    
                    lblCarConnectPerson.text = "暂无"
                    lblCarConnectPhone.text = "暂无"
                    lblCarPickerType.text = "暂无"
                    lblCarOrderTime.text = "暂无"
                    lblReturnCarAddressTitle.text = "店铺地址："
                    
                    if ((OrdersInfo?.objectForKey("dealer_info")) != nil) {
                         lblReturnCarAddress.text = OrdersInfo?.objectForKey("dealer_info")!.objectForKey("address") as? String
                    }
                   
                }
            }
            
            
            var strVIN:NSString
            if let vin = OrdersInfo?.objectForKey("car_info")?.objectForKey("vin"){
                strVIN = vin as! NSString
            }else{
                strVIN = ""
            }
            
            var strPlateNo:NSString
            if let plate_no = OrdersInfo?.objectForKey("car_info")?.objectForKey("plate_no"){
                strPlateNo = plate_no as! NSString
            }else{
                strPlateNo = ""
            }
            
            if strPlateNo.length > 0{
                  lblVIN.text = "车牌号：\(strPlateNo)      VIN码：\(strVIN)"
            }else{
                 lblVIN.text = "车牌号：暂未上牌      VIN码：\(strVIN)"
            }
            
            

        }else  if indexPath.row == 2{
            let lblServiceContentTitle = cell.contentView.viewWithTag(420) as! UILabel!
            let lblServiceContent = cell.contentView.viewWithTag(421) as! UILabel!
             let imgvLineThree = cell.contentView.viewWithTag(422) as! UIImageView!
            lblServiceContentTitle.text = "服务内容"
            if let content = OrdersInfo?.objectForKey("content") as! String!{
                lblServiceContent.text = content
            }else{
                lblServiceContent.text = nil
            }
            
            //                lblTitle.text = "保养内容："
            var strRepair : NSString
            if let repairContent = OrdersInfo?.objectForKey("content") as! String!{
                strRepair = repairContent
            }else{
                strRepair = ""
            }
            
            var strMaintain :NSString
            if let upkeepType = OrdersInfo?.objectForKey("upkeep_type") as! String!{
                var ary = upkeepType.componentsSeparatedByString(",")
                var aryName = ["机油","机油过滤器","空气滤清器","空调空气滤清器","冷却液","油滤器","燃油滤清器","火花塞","制动液","手动变速箱油","自动变速箱油","发动机正时皮带"]
                let str = NSMutableString()
                for i in 0 ..< ary.count{
                    let idx = ary[i]
                    
                    //                        var intVal:Int = idx.toint()
                    if idx != ""{
                        str.appendString(aryName[Int(idx)!])
                        str.appendString("、")
                    }
                }
                //                    var txt = str.replaceCharactersInRange(NSMakeRange(str.length - 1, 1), withString: "") as? String
                strMaintain = str.substringWithRange(NSMakeRange(0, str.length-1))
               
            }else{
                strMaintain = ""
               
            }
            
            if strRepair.length > 0 && strMaintain.length>0 {
                lblServiceContent.text = "维修：\(strRepair)\n保养：\(strMaintain)"
            }else if strRepair.length > 0 && strMaintain.length==0{
                lblServiceContent.text = strRepair as String
            }else  if strRepair.length == 0 && strMaintain.length>0{
                lblServiceContent.text =  strMaintain as String
            }
//            else{
//                lblServiceContent.text = strMaintain as String
//            }
            
            lblServiceContent.numberOfLines = 0
            /// 计算文本大小
            if lblServiceContent!.text != nil {
                let attributes = [NSFontAttributeName: lblServiceContent!.font]
                let option = NSStringDrawingOptions.UsesLineFragmentOrigin
                let text: NSString = NSString(CString: lblServiceContent!.text!.cStringUsingEncoding(NSUTF8StringEncoding)!,
                    encoding: NSUTF8StringEncoding)!
                var rect = text.boundingRectWithSize(CGSizeMake(tvList.frame.size.width-30, 1000), options: option, attributes: attributes, context: nil)
                rect.origin.x = 15
                rect.origin.y = 35
                cellRowThreeHeight = rect.size.height
                if rect.width>580{
                    lblServiceContent.numberOfLines = 10;
                    lblServiceContent.frame = CGRectMake(15, 30, tvList.frame.size.width-30, cellRowThreeHeight)
                }else{
                    lblServiceContent.numberOfLines = 10;
                    lblServiceContent.frame = CGRectMake(15, 35, tvList.frame.size.width-30, cellRowThreeHeight)
                }
                //                    var width = rect.size.width + 24
                //                     var height = rect.size.height + 80
                
                
            }
            
            let imgvY:CGFloat =  cellRowThreeHeight == 0 ? 64:cellRowThreeHeight+50>65 ?cellRowThreeHeight+50-1:64
            imgvLineThree.frame = CGRectMake(15, imgvY, tvList.frame.size.width-30, 1)
            

        }else  if indexPath.row == 3{
            let lblRemarkTitle = cell.contentView.viewWithTag(430) as! UILabel!
            let lblRemarkContent = cell.contentView.viewWithTag(431) as! UILabel!
            let lblPictureCount = cell.contentView.viewWithTag(436) as! UILabel!
            let imgvPicture  = cell.contentView.viewWithTag(432) as! SWImageView
            let  btnShowImage = cell.contentView.viewWithTag(434) as! UIButton!
            let  btnVoice = cell.contentView.viewWithTag(435) as! UIButton!
            
            let photoList = OrdersInfo?.objectForKey("photo_list") as! NSArray!
            if photoList?.count > 0 {
                imgvPicture.frame = CGRectMake(15, 15, 75, 75)
                imgvPicture.loadURL(photoList[0].objectForKey("smallUrl") as? String)
                //                    imgvPicture.image = UIImage(named: photoList.objectAtIndex(0) as! String)
                btnShowImage.hidden = false
                btnShowImage.frame = CGRectMake(15, 15, 75, 75)
                lblRemarkTitle.frame = CGRectMake(110, 15, 100, 13)
                lblRemarkContent.frame = CGRectMake(110, 35, 500, 13)
                btnVoice.frame = CGRectMake(110, 71, 95, 28)
                lblPictureCount.text = "查看全部\(photoList.count)张照片"
                
            }
            else{
                btnShowImage.hidden = true
                imgvPicture.frame = CGRectMake(15, 15, 0, 0)
                imgvPicture.image = nil
                lblPictureCount.text = ""
                lblRemarkTitle.frame = CGRectMake(15, 15, 100, 13)
                lblRemarkContent.frame = CGRectMake(15, 35, 594, 13)
                btnVoice.frame = CGRectMake(15, 71, 95, 28)
                
            }
            
            if player != nil{
                btnVoice.hidden = false
                if (OrdersInfo?.objectForKey("audio") as! String!) != nil{
//                    var path = audio.lastPathComponent.temporaryPath()
                    
//                    btnVoice.setTitle(String(format: "%.0lf\"", player!.duration), forState: .Normal)
                }
                
                
            }else{
                if btnVoice != nil{
                    btnVoice.hidden = true
                }
            }
            var strGetCarRemark : NSString
            if let userGetRemark = OrdersInfo?.objectForKey("getcar_info")?.objectForKey("remark") as! String!{
//                lblRemarkContent.text = userRemark
                strGetCarRemark = userGetRemark
            }else{
//                lblRemarkContent.text = "未填写备注"
                strGetCarRemark = ""
            }
            
            var strSendCarRemark : NSString
            if let userSendRemark = OrdersInfo?.objectForKey("sendcar_info")?.objectForKey("remark") as! String!{
                strSendCarRemark = userSendRemark
            }else{
                strSendCarRemark = ""
            }
            
            if strGetCarRemark.length > 0 && strSendCarRemark.length > 0 {
//                lblRemarkContent.text = "取车备注：\(strGetCarRemark)，送车备注：\(strSendCarRemark)"
                lblRemarkTitle.text = "送车备注"
                lblRemarkContent.text = "\(strSendCarRemark)"
            }else if strGetCarRemark.length > 0 && strSendCarRemark.length == 0 {
                lblRemarkTitle.text = "取车备注"
                lblRemarkContent.text = "\(strGetCarRemark)"
            }else if strGetCarRemark.length == 0 && strSendCarRemark.length > 0 {
                lblRemarkTitle.text = "送车备注"
                lblRemarkContent.text = "\(strSendCarRemark)"
            }else{
                lblRemarkTitle.text = "用户备注"
                 lblRemarkContent.text = "未填写备注"
            }
            
            /// 计算文本大小
            if lblRemarkContent!.text != nil {
                let attributes = [NSFontAttributeName: lblRemarkContent!.font]
                let option = NSStringDrawingOptions.UsesLineFragmentOrigin
                let text: NSString = NSString(CString: lblRemarkContent!.text!.cStringUsingEncoding(NSUTF8StringEncoding)!,
                    encoding: NSUTF8StringEncoding)!
                var rect = text.boundingRectWithSize(CGSizeMake(lblRemarkContent.frame.size.width, 40), options: option, attributes: attributes, context: nil)
                rect.origin.x = lblRemarkContent.frame.origin.x
                rect.origin.y = 35
                
                if rect.width>lblRemarkContent.frame.size.width-20{
                    lblRemarkContent.numberOfLines = 2;
                    lblRemarkContent.frame = CGRectMake(lblRemarkContent.frame.origin.x, 30, lblRemarkContent.frame.size.width, 40)
                }else{
                    lblRemarkContent.numberOfLines = 1;
                    lblRemarkContent.frame = CGRectMake(lblRemarkContent.frame.origin.x, 35, lblRemarkContent.frame.size.width, 15)
                }
                //                    var width = rect.size.width + 24
                //                     var height = rect.size.height + 80
                
                
            }

        }else  if indexPath.row == 4{
            let lblPersonnameTitle = cell.contentView.viewWithTag(600) as! UILabel
            let imageLine = cell.contentView.viewWithTag(610) as! UIImageView
            let lblPersonPhotoTitle = cell.contentView.viewWithTag(620) as! UILabel
            let lblPersonname = cell.contentView.viewWithTag(640) as! UILabel
            let lblPersonPhone = cell.contentView.viewWithTag(680) as! UILabel
            if let ary = OrdersInfo?.objectForKey("status_detail") as! NSArray!{
                for i in 0 ..< 16{
//                    let lblOrderStatusTitle = cell.contentView.viewWithTag(440) as! UILabel
//                    let lbl1 = cell.contentView.viewWithTag(450+i) as! UILabel
//                    let lbl2 = cell.contentView.viewWithTag(470+i) as! UILabel
//                    
//                    let imgvLine = cell.contentView.viewWithTag(500+i) as! UIImageView
//                    let imgvNumber = cell.contentView.viewWithTag(520+i) as! UIImageView
//                    let lbl3 = cell.contentView.viewWithTag(540+i) as! UILabel
//                    
//                    let imgvPersonBG = cell.contentView.viewWithTag(560+i) as! UIImageView
//                    let imgvPersonPhoto = cell.contentView.viewWithTag(580+i) as! SWImageView
//                    
                    
//
//                    imgvLine.backgroundColor = UIColor(white: 0.92, alpha: 1)
//                    let y1 = CGFloat(50+55*i)
//                    let y2 = CGFloat(70+55*i)
//                    let y3 = CGFloat(40+55*i)
//                    //                        frame = lbl1.frame
//                    lbl1.frame = CGRectMake(65, y1, lbl1.frame.size.width, lbl1.frame.size.height)
//                    lbl2.frame = CGRectMake(65, y2, lbl2.frame.size.width, lbl2.frame.size.height)
//                    lbl3.frame = CGRectMake(15, y1, lbl3.frame.size.width, lbl3.frame.size.height)
//                    imgvNumber.frame = CGRectMake(15, y1, imgvNumber.frame.size.width, imgvNumber.frame.size.height)
//                    imgvLine.frame = CGRectMake(15+17, y3, imgvLine.frame.size.width, imgvLine.frame.size.height)
//                    lblOrderStatusTitle.text = "订单状态"
                    
                    if i < ary.count{
                        //                            var ary = OrdersInfo?.objectForKey("status_detail")
                        let dicStatus = ary.objectAtIndex((ary.count)-i-1)
//                        let head = dicStatus.objectForKey("head") as! String!
//                        let date = dicStatus.objectForKey("date")  as! String!
//                        let msg = dicStatus.objectForKey("msg")  as! String!
                        
                        let status = dicStatus.objectForKey("status") as! String!
                        if status == "6"{
//                            imgvPersonBG.hidden = false
//                            imgvPersonBG.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
//                            
//                            if let head_photo = OrdersInfo?.objectForKey("getcar_info")?.objectForKey("head_photo") {
//                                imgvPersonPhoto.loadURL(OrdersInfo?.objectForKey("getcar_info")?.objectForKey("head_photo")?.objectForKey("small_url") as! String)
//                            }else{
//                                imgvPersonPhoto.image = UIImage(named: "photo.png")
//                            }
                            lblPersonnameTitle.hidden = false
                            lblPersonPhotoTitle.hidden = false
                            lblPersonname.hidden = false
                            lblPersonPhone.hidden = false
                            
                            lblPersonnameTitle.text = "取车员"
                            lblPersonPhotoTitle.text = "联系电话"
                            
                            if let c_name = OrdersInfo?.objectForKey("getcar_info")?.objectForKey("name") as! String!{
                                lblPersonname.text = c_name
                            }else{
                                lblPersonname.text = nil
                            }
                            
                            if let c_phone = OrdersInfo?.objectForKey("getcar_info")?.objectForKey("telephone") as! String!{
                                lblPersonPhone.text = c_phone
                            }else{
                                lblPersonPhone.text = nil
                            }
                            
                        }else if status == "11"{
//                            imgvPersonBG.hidden = false
//                            imgvPersonBG.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1)
//                            if let head_photo = OrdersInfo?.objectForKey("sendcar_info")?.objectForKey("head_photo") {
//                                imgvPersonPhoto.loadURL(OrdersInfo?.objectForKey("sendcar_info")?.objectForKey("head_photo")?.objectForKey("small_url") as! String)
//                            }else{
//                                imgvPersonPhoto.image = UIImage(named: "photo.png")
//                            }
                            
                            lblPersonnameTitle.hidden = false
                            lblPersonPhotoTitle.hidden = false
                            lblPersonname.hidden = false
                            lblPersonPhone.hidden = false
                            imageLine.hidden = false
                            
                            lblPersonnameTitle.text = "送车员"
                            lblPersonPhotoTitle.text = "联系电话"
                            
                            if let c_name = OrdersInfo?.objectForKey("sendcar_info")?.objectForKey("name") as! String!{
                                lblPersonname.text = c_name
                            }else{
                                lblPersonname.text = nil
                            }
                            
                            if let c_phone = OrdersInfo?.objectForKey("sendcar_info")?.objectForKey("telephone") as! String!{
                                lblPersonPhone.text = c_phone
                                lblPersonPhone.textColor = ORANGECOLOR
                            }else{
                                lblPersonPhone.text = nil
                            }
                            
                            
                        }else{
//                            imgvPersonBG.hidden = true
//                            imgvPersonPhoto.hidden = true
//                            imgvPersonPhoto.image = nil
                            lblPersonnameTitle.hidden = true
                            lblPersonPhotoTitle.hidden = true
                            lblPersonname.hidden = true
                            lblPersonPhone.hidden = true
                            imageLine.hidden = true
                        }
                        
//                        lbl1.text = "\(head) (\(date))"
//                        lbl2.text = "\(msg)"
//                        lbl1.hidden = false
//                        lbl2.hidden = false
//                        lbl3.hidden = false
//                        lbl3.text = "\((ary.count)-i)"
//                        imgvLine.hidden = false
//                        imgvNumber.hidden = false
//                        if i == 0 {
//                            imgvNumber.image = UIImage(named: "bule_circle.png")
//                            lbl3.textColor = UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
//                            imgvLine.frame = CGRectMake(17+15, 85, 1, 10)
//                        }else if i == ary.count-1{
//                            imgvNumber.image = UIImage(named: "gary_circle.png")
//                            lbl3.textColor = UIColor.grayColor()
//                            
//                            imgvLine.frame = CGRectMake(17+15, y3, 1, 10)
//                        }else{
//                            imgvNumber.image = UIImage(named: "gary_circle.png")
//                            lbl3.textColor = UIColor.grayColor()
//                            imgvLine.frame = CGRectMake(17+15, y3, 1, 55)
//                        }
                        
                    }else{
//                        lbl1.hidden = true
//                        lbl2.hidden = true
//                        lbl3.hidden = true
//                        imgvNumber.hidden = true
//                        imgvLine.hidden = true
                    }
                }
            }else{
//                for i in 0 ..< 16{
//                    let lbl1 = cell.contentView.viewWithTag(450+i) as! UILabel
//                    let lbl2 = cell.contentView.viewWithTag(470+i) as! UILabel
//                    
//                    let imgvLine = cell.contentView.viewWithTag(500+i) as! UIImageView
//                    let imgvNumber = cell.contentView.viewWithTag(520+i) as! UIImageView
//                    let lbl3 = cell.contentView.viewWithTag(540+i) as! UILabel
//                    
//                    lbl1.hidden = true
//                    lbl2.hidden = true
//                    lbl3.hidden = true
//                    imgvNumber.hidden = true
//                    imgvLine.hidden = true
//                }
            }

        }

            cell.selectionStyle = UITableViewCellSelectionStyle.None
            return cell
            
//        }
        
//        var cell = tableView.dequeueReusableCellWithIdentifier(identifierStatus) as UITableViewCell!
//        if cell == nil {
//            cell = UITableViewCell(style: .Default, reuseIdentifier: identifierDetail)
//            var lbl = UILabel.create(CGRectMake(90,30,1024-360-120,20), font: UIFont.systemFontOfSize(18), textColor: UIColor(red: 0.06, green: 0.48, blue: 0.78, alpha: 1))
//            lbl.tag = 100;
//            cell.contentView.addSubview(lbl);
//            
//            lbl = UILabel.create(CGRectMake(90,60,1024-360-120,20), font: UIFont.systemFontOfSize(18), textColor: UIColor.grayColor())
//            lbl.tag = 101;
//            cell.contentView.addSubview(lbl);
//            
//            var imgv = UIImageView.init(frame: CGRectMake(28, 30, 20, 20))
//            imgv.backgroundColor = UIColor(red: 0.16, green: 0.50, blue: 0.78, alpha: 1)
//            imgv.layer.cornerRadius = 10.0
//            imgv.tag = 102;
//            cell.contentView.addSubview(imgv)
//            
//            imgv = UIImageView.init(frame: CGRectMake(38, 55, 1, 45))
//            imgv.backgroundColor = UIColor(red: 0.16, green: 0.50, blue: 0.78, alpha: 1)
//            imgv.tag = 103;
//            cell.contentView.addSubview(imgv)
//            
//            lbl = UILabel.create(CGRectMake(28,30,20,20), font: UIFont.systemFontOfSize(15), textColor: UIColor.whiteColor())
//            lbl.textAlignment = NSTextAlignment.Center
//            lbl.tag = 104;
//            cell.contentView.addSubview(lbl);
//            
//        }
//        
//        let lblStatus = cell.contentView.viewWithTag(100) as! UILabel
//        let lblStatusDetail = cell.contentView.viewWithTag(101) as! UILabel
//        let lblNumber = cell.contentView.viewWithTag(104) as! UILabel
//        
//        var ary = OrdersInfo?.objectForKey("status_detail")
//        let dicStatus = ary?.objectAtIndex((ary?.count)!-indexPath.row-1)
//        let head = dicStatus?.objectForKey("head") as! String!
//        let date = dicStatus?.objectForKey("date")  as! String!
//        let msg = dicStatus?.objectForKey("msg")  as! String!
//        
//        lblStatus.text = "\(head) (\(date))"
//        lblStatusDetail.text = "\(msg)"
//        //        lblNumber.text = "\(indexPath.row+1)"
//        lblNumber.text = "\((ary?.count)!-indexPath.row)"
//        //        tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
//        cell.selectionStyle = UITableViewCellSelectionStyle.None
//        
//        return cell
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        let processDic = OrdersInfo?.objectForKey("process")
        
        if strShowType == "1"{
            if indexPath.row == 0{
                return 65
            }else if indexPath.row == 1{
                
                if let order_status = OrdersInfo?.objectForKey("order_status")?.integerValue as Int!{
                    if order_status == 1 || order_status == 3 || order_status == 13 || processDic == nil{
                    } else if carstatus != nil{
                        if carstatus == 0 || carstatus == 1 || carstatus == 2 {
                            return 230
                        } else if carstatus == 3 || carstatus == 4 || carstatus == 5 {
                            return 250
                        } else {
                            return 250
                        }
                    }
                }
                
                return 125
            }else if indexPath.row == 2{
                if let order_status = OrdersInfo?.objectForKey("order_status")?.integerValue as Int!{
                    if order_status == 1 || order_status == 3 || order_status == 13 || processDic == nil{
                    } else if carstatus != nil{
                        if carstatus < 3 {
                            return 90
                        } else {
                            return cellRowThreeHeight == 0 ? 65:cellRowThreeHeight+50>65 ?cellRowThreeHeight+50:65
                        }
                    }
                }
                return cellRowThreeHeight == 0 ? 65:cellRowThreeHeight+50>65 ?cellRowThreeHeight+50:65
            }else if indexPath.row == 3{
                if let order_status = OrdersInfo?.objectForKey("order_status")?.integerValue as Int!{
                    if order_status == 1 || order_status == 3 || order_status == 13 || processDic == nil{
                    } else if carstatus != nil{
                        if carstatus < 3 {
                            return 90
                        } else {
                            return 200
                        }
                    }
                }
                return 110
            }
            
            return CGFloat(20+40+55*(aryStatusDetail == nil ? 0 :aryStatusDetail?.count)!)
        }else{
            return 80
        }
        
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if strShowType == "1"{
            return 5
        }else{
            let ary = OrdersInfo?.objectForKey("status_detail")
            if ary?.count > 0{
                return (ary?.count)!
            }
        }
        return 0
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let v = UIView(frame: CGRectMake(0, 0, 1024-370-30, 72))
        v.backgroundColor = UIColor.whiteColor()
        
        let imgvCar = UIImageView.init(frame: CGRectMake(15, 15, 60, 42))
        imgvCar.image = UIImage(named: "picture.png")
        v.addSubview(imgvCar)
        
        let imageViewScan = UIImageView(frame: CGRectMake(1024-370-30-57, 15, 42, 42))
//            self.createQRForString("好好", qrImageName: "") //“ocrBack” 放入二维码中间图片的名字
        v.addSubview(imageViewScan)
        
        var titles = "车辆型号，行驶里程，订单状态，预约单号".componentsSeparatedByString("，")
        for i in 0 ..< titles.count{
            let col = i % 4
            let row = i / 4
            var lbl = UILabel.create(CGRectMake(CGFloat(90+100*col), CGFloat(20+35*row), 100, 13), font: UIFont.systemFontOfSize(12), textColor:UIColor.grayColor())//UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1)
            lbl.text = titles[i]
            v.addSubview(lbl)
            
            lbl = UILabel.create(CGRectMake(CGFloat(90+100*col), CGFloat(38+35*row), 100, 15), font: UIFont.systemFontOfSize(15), textColor: UIColor.blackColor())
//            lbl.text = titles[i]
            lbl.tag = 100+i
            v.addSubview(lbl)
        }
        
        //标题名字
        let lblCarType = v.viewWithTag(100) as! UILabel!
        let lblMeter = v.viewWithTag(101) as! UILabel!
        let lblStatus = v.viewWithTag(102) as! UILabel!
        let lblNumber = v.viewWithTag(103) as! UILabel!
        lblNumber.frame = CGRectMake(390, 38, 150, 15)
        
        if let strCarType = OrdersInfo?.objectForKey("car_info")?.objectForKey("abbr_name"){
            lblCarType.text = "\(strCarType)"
        }else{
            lblCarType.text = nil
        }
        
        if let mileage = OrdersInfo?.objectForKey("mileage") as! String!{
            lblMeter.text = "\(mileage) KM"
        }else{
            lblMeter.text = "-- KM"
        }

        let imgvLine = UIImageView.init(frame: CGRectMake(15, 71, v.frame.size.width-30, 1))
        imgvLine.image = UIImage(named: "dashe_ line.png")
//        imgvLine.backgroundColor = UIColor.grayColor()
        v.addSubview(imgvLine)
        
//        lblOrderNumber = UILabel.create(CGRectMake(29, 25, 300, 25), font: UIFont.systemFontOfSize(23), textColor: UIColor.grayColor())
//        //        lblTitle.text = "订单号：D1620150807001A"
//        v.addSubview(lblOrderNumber)
        
        if let checkNo = OrdersInfo?.objectForKey("check_no") as! String!{
            lblNumber.text = checkNo
            imageViewScan.image = self.creatQRForString(checkNo, qrImageName: "")
        }else{
            lblNumber.text = nil
        }
        
        var btn = UIButton(type: .Custom)
        btn.frame = CGRectMake(530, 20, 40, 40)
        btn.setImage(UIImage(named: "left"), forState: .Normal)
        btn.addTarget(self, action: #selector(SHOrdersListView.startButtonClicked(_:)), forControlEvents: .TouchUpInside)
        v.addSubview(btn)
        btn.hidden = true
        btn.tag = 100
        
        btn = UIButton(type: .Custom)
        btn.frame = CGRectMake(570, 20, 40, 40)
        btn.setImage(UIImage(named: "right"), forState: .Normal)
        btn.addTarget(self, action: #selector(SHOrdersListView.startButtonClicked(_:)), forControlEvents: .TouchUpInside)
        v.addSubview(btn)
        btn.hidden = true
        btn.tag = 101
        
        
        btnMore = UIButton(type: .Custom)
        btnMore.frame = CGRectMake(610, 20, 40, 40)
        btnMore.setImage(UIImage(named: "other"), forState: .Normal)
        btnMore.addTarget(self, action: #selector(SHOrdersListView.startButtonClicked(_:)), forControlEvents: .TouchUpInside)
        v.addSubview(btnMore)
        btnMore.tag = 102
        
        let orderStatus =  strOrderStatus?.intValue
        if orderStatus == 1 || orderStatus == 3  || orderStatus == 14 {
            btnMore.hidden = false
        }else{
            btnMore.hidden = true
        }
        btnMore.hidden = true
        
//        lblOrderStatus = UILabel.create(CGRectMake(29, 70, 400, 25), font: UIFont.systemFontOfSize(23), textColor: UIColor(red: 0, green: 0.42, blue: 0.85, alpha: 1))
        
        let ary = OrdersInfo?.objectForKey("status_detail")
        if ary?.count > 0{
//            lblOrderStatus.text = ary!.objectAtIndex(ary!.count-1).objectForKey("head") as! String!
            lblStatus.textColor = ORANGECOLOR
            lblStatus.text = ary!.objectAtIndex(ary!.count-1).objectForKey("head") as! String!
        }
        
        if let order_status = OrdersInfo?.objectForKey("order_status")?.integerValue as Int!{
            if 1 == order_status{
                lblStatus.text  = "已发起"
            }else if 2 == order_status{
                lblStatus.text  = "未成功"
            }else if 3 == order_status{
                lblStatus.text  = "已接受"
            }else if 4 == order_status{
                if let back_type = OrdersInfo?.objectForKey("back_type")?.integerValue as Int! {
                    if back_type == 1 {
                        lblStatus.text = "待评价"
                        
                    }else{
                        lblStatus.text = "待评价"
                    }
                }
                
            }else if 5 == order_status{
                lblStatus.text  = "已评价"
            }else if 6 == order_status{
                lblStatus.text  = "待取车"
            }else if 7 == order_status{
                lblStatus.text  = "服务中"
            }else if 8 == order_status{
                lblStatus.text  = "服务中"
            }else if 9 == order_status{
                if  let pick_type = OrdersInfo?.objectForKey("pick_type")?.integerValue as Int!{
                    if pick_type == 1{
                        lblStatus.text  = "服务中"//待付款
                    }else{
                        lblStatus.text  = "服务中"
                        
                    }
                }
                
            }else if 10 == order_status{
                lblStatus.text  = "待付款"
            }else if 11 == order_status{
                if let back_type = OrdersInfo?.objectForKey("back_type")?.integerValue as Int! {
                    if back_type == 1 {
                        lblStatus.text = "待送车"//待评价
                        
                    }else{
                        lblStatus.text = "待送车"
                    }
                }
            }else if 12 == order_status{
                lblStatus.text  = "服务中"//待评价
            }else if 13 == order_status{
                lblStatus.text  = "已取消"
            }else if 14 == order_status{
                lblStatus.text  = "待确认"
            }else if 15 == order_status{
                lblStatus.text  = "已接受"
            }else if 16 == order_status{
                lblStatus.text  = "付款中"
            }
            
        }
        
//        v.addSubview(lblOrderStatus)
        
        
//        var vLine = UIView (frame: CGRectMake(0 , 121, frame.size.width, 1))
//        vLine.backgroundColor = UIColor(white: 0.90, alpha: 1)
//        v.addSubview(vLine)
//
//        var vLine1 = UIView (frame: CGRectMake(0 , 160, frame.size.width, 1))
//        vLine1.backgroundColor = UIColor(white: 0.90, alpha: 1)
//        v.addSubview(vLine1)
//
//        btn = UIButton(type: .Custom)
//        btn.frame = CGRectMake(0, 121, frame.size.width/2.0, 40)
//        btn.setTitle("订单详情", forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
//        btn.setTitleColor(UIColor.grayColor(), forState:.Normal)
//        btn.addTarget(self, action: "startButtonClicked:", forControlEvents: .TouchUpInside)
//        v.addSubview(btn)
//        btn.tag = 103
//        
//        
//        btn = UIButton(type: .Custom)
//        btn.frame = CGRectMake(frame.size.width/2.0, 121, frame.size.width/2.0, 40)
//        btn.setTitle("订单状态", forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
//        btn.setTitleColor(UIColor.grayColor(), forState:.Normal)
//        btn.addTarget(self, action: "startButtonClicked:", forControlEvents: .TouchUpInside)
//        v.addSubview(btn)
//        btn.tag = 104
//        
//        let vLine2 = UIView (frame: CGRectMake(frame.size.width/2.0 , 123, 2, 36))
//        vLine2.backgroundColor = UIColor(white: 0.90, alpha: 1)
//        v.addSubview(vLine2)
        
        return v
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 71 //161
    }
    
    func imageClicked(sender: UIButton){
        
        
        let index = sender.superview!.tag % 100
        
        if let photoList = OrdersInfo?.objectForKey("photo_list") as! [NSDictionary]!{
            if index < photoList.count{
                let vc = UIViewController()
                vc.view.frame = CGRectMake(0, 0, 500, 500)
                let imgv = SWImageView(frame: CGRectMake(0, 0, 500, 500))
                vc.view.addSubview(imgv)
                
                if let originalURL = photoList[index].objectForKey("originalUrl") as? String{
                    imgv.loadURL(originalURL)
                    pop = nil
                    pop = UIPopoverController(contentViewController: vc)
                    pop.popoverContentSize = CGSizeMake(500, 500)
                    pop.presentPopoverFromRect(sender.frame, inView: sender.superview!, permittedArrowDirections: .Any, animated: true)
                }
                
            }
        }

    }
    
    
    func btnShowPictureClicked(sender:UIButton){
//        //网络相册：
//        //创建SwiftPPT
//        let pptView2 = CFPPTView(type: PPTType.netWork) { () -> [PPTDataModel] in
//            
//            var networkImages = ["http://img.netbian.com/file/2015/0619/e8ffa0a298a4f7374df0e599c4fa134d.jpg"]
//            let photoList = self.OrdersInfo?.objectForKey("photo_list") as! NSArray!
//            
//            
////            var networkTitleStr = ["","","网络幻灯:小黄人三","网络幻灯:小黄人四","网络幻灯:小黄人五"]
//            
//            var dataModels: [PPTDataModel] = Array()
//            
//            for i in 0..<photoList.count {
//                
//                let dataModel = PPTDataModel(networkImageUrl:(photoList[i].objectForKey("original_url") as! String), placeHolderImage:nil, titleStr:"" ,model: nil)
//                dataModels.append(dataModel)
//            }
//            
//            return dataModels
//        }
//        
//        pptView2.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width,UIScreen.mainScreen().bounds.height)
//        pptView2.backgroundColor = UIColor.blackColor()
//        self.window?.addSubview(pptView2)
        
//            .addSubview(pptView2)
//        let vc = UIViewController()
//        vc.view.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
//        vc.view.addSubview(pptView2)
//        pop = nil
//        pop = UIPopoverController(contentViewController: vc)
//        pop.popoverContentSize = CGSizeMake(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)
//        pop.presentPopoverFromRect(sender.frame, inView: sender.superview!, permittedArrowDirections: .Any, animated: true)
//        let photoList = self.OrdersInfo?.objectForKey("photo_list") as! NSArray!
//        let pohtoBrowing:PhotoBrowing = PhotoBrowing(frame:CGRectMake(0,0,UIScreen.mainScreen().bounds.width,UIScreen.mainScreen().bounds.height))
//        if photoList?.count > 0 {
//            for i in 0 ..< 5{
//                
//                if i < photoList.count{
//                    pohtoBrowing.imageView.loadURL(photoList[i].objectForKey("small_url") as! String)
//                }else{
//                    pohtoBrowing.imageView.image = nil
//                }
//            }
//        }
//        self.addSubview(pohtoBrowing)
//        UIView.animateWithDuration(0.3, animations:{
//            pohtoBrowing.backgroundColor = UIColor(white:0, alpha: 1)
//            pohtoBrowing.imageView.alpha = 1
//        })
//        
//        
//        print("浏览照片")
        let photoList = self.OrdersInfo?.objectForKey("photo_list") as! NSArray!
                    let browser = SDPhotoBrowser.init()
                    browser.sourceImagesContainerView = self;
                    browser.imageCount = photoList.count
                    browser.currentImageIndex = 0
                    browser.delegate = self
                    browser.show()
//        if photoList?.count > 0 {
//            var photoGroup = SDPhotoGroup.init()
//            var temp = NSMutableArray()
//            
//           photoList.enumerateObjectsUsingBlock({ (var dic, idx, have) -> Void in
//            for idx in 0 ..< photoList.count{
//                dic = photoList[idx]
//                var item = SDPhotoItem.init()
//                item.thumbnail_pic = dic.objectForKey("original_url") as! String
//                temp.addObject(item)
//                
//            }
//           })
//            photoGroup.photoItemArray = temp.copy() as! [AnyObject]
//            self.addSubview(photoGroup)
//        }
        
    }
    
    func photoBrowser(browser: SDPhotoBrowser!, highQualityImageURLForIndex index: Int) -> NSURL! {
        let photoList = self.OrdersInfo?.objectForKey("photo_list") as! NSArray
        let urlstr = photoList[index].objectForKey("originalUrl") as! String
        return NSURL(string: urlstr)
    }
    
    func photoBrowser(browser: SDPhotoBrowser!, placeholderImageForIndex index: Int) -> UIImage! {
        return nil
        
    }
    
    
    func audioClicked(){
        if let audioPlayer = player{
            if audioPlayer.playing{
                btnPlay?.selected = false
                audioPlayer.stop()
               
            }else{
                audioPlayer.play()
                btnPlay?.selected = true
            }
            //            tvList.reloadData()
        }
    }
    
    
    func updateAllOrdersInfo(){
        ordersListDelegate?.allOrdesUpdated(OrdersInfo)
        tvList.reloadData()
        
    }
    
    //  MARK: - SWImageView Delegate
    func swImageViewLoadFinished(imgv: SWImageView) {
        
    }
    //  MARK: - AVAudioPlayer Delegate
    func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
        btnPlay?.selected = false
    }
    
    //  MARK: - UIPopoverController Delegate
    func popoverControllerShouldDismissPopover(popoverController: UIPopoverController) -> Bool {
        return true
    }
    
    func popoverControllerDidDismissPopover(popoverController: UIPopoverController) {
        let picker: UIDatePicker = popoverController.contentViewController.view.viewWithTag(100) as! UIDatePicker
        btnChangeTime.userInteractionEnabled = true
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        if let orderTime = OrdersInfo?.objectForKey("order_time") as? String{
            let date = formatter.dateFromString(orderTime)
            formatter.dateFormat = "yyyy-MM-dd HH:mm"
            
            if formatter.stringFromDate(picker.date) != formatter.stringFromDate(date!){
                selectedDate = picker.date
                
                let alert = UIAlertView(title: nil, message: "是否确定要变更预约时间？", delegate: self, cancelButtonTitle: "取消")
                alert.addButtonWithTitle("确定")
                alert.tag = 100
                alert.show()
            }
        }
        
    }
    
    
    // MARK: - UIAlertView Delegate
    func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
        let buttonTitle = alertView.buttonTitleAtIndex(buttonIndex)
        if alertView.tag == 100{
            if buttonTitle == "确定"{
                SWUtils.RunOnBackgroundThread { () -> Void in
                    autoreleasepool({ () -> () in
                        let dict = SHDataProvider.adjustOrder(["order_id":self.OrdersInfo?.objectForKey("order_id") as! String!,"order_time":String(format: "%.0lf",self.selectedDate!.timeIntervalSince1970)])
                        
                        SWUtils.RunOnMainThread({ () -> Void in
                            if let code = dict?.objectForKey("code")?.intValue{
                                if code==200{
                                    UIAlertView.show(nil, message: "操作成功", cancelButton: nil)
                                    
                                    let mut = NSMutableDictionary(dictionary: self.OrdersInfo!) as NSMutableDictionary
                                    mut.setObject("14", forKey: "order_status")
                                    let formatter = NSDateFormatter()
                                    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
                                    mut.setObject(formatter.stringFromDate(self.selectedDate!), forKey: "order_time")
                                    
                                    //                                self.OrdersInfo = NSDictionary(dictionary: mut)
                                    self.OrdersInfo = NSDictionary(dictionary: (dict?.objectForKey("data"))! as! [NSObject : AnyObject])
                                    self.updateAllOrdersInfo()
                                }else{
                                    if let msg = dict?.objectForKey("message") as? String{
                                        UIAlertView.show(nil, message: msg, cancelButton: nil)
                                    }else{
                                        UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                                    }
                                }
                            }else{
                                UIAlertView.show(nil, message: "操作失败", cancelButton: nil)
                            }
                            //                        self.getNewOrderDetail()
                            //                        self.updateAllOrdersInfo()
                        })
                        
                    })
                }
            }
        }else if alertView.tag == 1000{
            if buttonIndex == 1{
                self.alreadyPay()
            }else{
                btnReset.userInteractionEnabled = true
            }
        }
    }

    
    func scrollViewDidScroll(scrollView: UIScrollView) {
         if (scrollView.contentOffset.y < -100){
            
        }
    }

    

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
